diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7339432..ee2c173 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,7 +5,6 @@ - { Log.d("debug", "currentbutton, 現在地にカメラを移動"); + //shopsViewModel.setViewArea(currentLatlng.longitude+20, currentLatlng.latitude+20.0, currentLatlng.longitude-20.0, currentLatlng.latitude-20.0); + System.out.println(shopsViewModel.getShopsLiveData().getValue()); //現在地にカメラを移動 zoomMap(currentLatlng.latitude, currentLatlng.longitude); }); @@ -105,10 +116,9 @@ mMap = googleMap; //ViewModelへのアクセス - ShopsViewModel shopsViewModel = new ViewModelProvider(this).get(ShopsViewModel.class); + shopsViewModel = new ViewModelProvider(this).get(ShopsViewModel.class); //ダミーショップaとcが返される座標にしておく - //画面スクロールに応じて以下の1行を実行するようにする - shopsViewModel.setViewArea(20.0, 40.0, 10.0, 30.0); + shopsViewModel.setViewArea(40.0, 140.0, 20.0, 100.0); // LiveDataへの購読 shopsViewModel.getShopsLiveData().observe(this, shops -> { @@ -116,6 +126,7 @@ //対応するMarkerがなければMarkerを立てる for(Shop shop:shops) { if(shopsViewModel.getMarker(shop)==null){ + shopLatlng = new LatLng(shop.getLatitude(), shop.getLongitude()); System.out.println(shopLatlng); Marker createMaker = mMap.addMarker(new MarkerOptions().position(shopLatlng).title("")); @@ -138,38 +149,45 @@ testLatlng2 = new LatLng(34.74, 135.26); //初期画面の座標(現在地をロードするまで) -// initialLatlng = new LatLng(39,138); + initialLatlng = new LatLng(39,138); //店Cの座標にカメラ移動 - initialLatlng = new LatLng(34.5,12.3); +// initialLatlng = new LatLng(34.5,12.3); //初期画面に移動 -// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatlng, 5f)); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatlng, 15f)); //標準のマーカー(店用のピン) - setMarker(latitude, longitude); + //setMarker(latitude, longitude); //アイコン画像をマーカーに設定 //画像関連がわかっていないのでパス //setIcon(latitude, longitude); + //画面が動いたとき + mMap.setOnCameraIdleListener(() -> { + //カメラの座標とZOOM倍率を保存 + nowCamera = mMap.getCameraPosition(); + nowLatlng = new LatLng(nowCamera.target.latitude, nowCamera.target.longitude); + nowZoom = nowCamera.zoom; + //Shopの描画範囲を指定 + shopsViewModel.setViewArea(nowLatlng.longitude+1, nowLatlng.latitude+1, nowLatlng.longitude-1, nowLatlng.latitude-1); + }); + /* //多分使わない // タップした時のリスナーをセット mMap.setOnMapClickListener(tapLocation -> { // map(ピン以外)をtapされた位置の緯度経度 - latlng = new LatLng(tapLocation.latitude, tapLocation.longitude); - String str = String.format(Locale.US, "%f, %f", tapLocation.latitude, tapLocation.longitude); - mMap.addMarker(new MarkerOptions().position(location).title(str)); - mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 18)); + tapLatlng = new LatLng(tapLocation.latitude, tapLocation.longitude); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(tapLatlng, nowZoom)); + shopsViewModel.setViewArea(tapLatlng.longitude+1, tapLatlng.latitude+1, tapLatlng.longitude-1, tapLatlng.latitude-1); }); - */ + */ //長押し時に店を作成し、その座標にピンを立てる //長押し時にその座標を保存し、投稿画面に移り、Shop作成完了時にはピンを立て、Shop画面に移行 mMap.setOnMapLongClickListener(longpushLocation -> { - // Nemophila nemophila = (Nemophila) this.getApplication(); //長押しされた位置の緯度経度を取得 //LatLng newlocation = new LatLng(longpushLocation.latitude, longpushLocation.longitude); //Nemophilaに座標を保存 @@ -208,6 +226,9 @@ nemophila.setCurrentShop((Shop)marker.getTag()); System.out.println(nemophila.getCurrentShop().getName()); + //ShopActivity画面に遷移 + Intent intent = new Intent(MainActivity.this, ShopActivity.class); + startActivity(intent); return false; } @@ -229,9 +250,6 @@ markerOptions2.title(null); mMap.addMarker(markerOptions2); - // ズーム - //zoomMap(latitude, longitude); - } private void zoomMap(double latitude, double longitude){ @@ -258,7 +276,7 @@ newLatLngBounds(bounds, width, height, 0)); //ズーム処理 - mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15f)); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, nowZoom)); } //現在地の表示 @@ -356,7 +374,7 @@ public void onLocationChanged(Location location) { //初期画面は現在地を中心にするため... if (currentLatlng == null){ - //zoomMap(location.getLatitude(), location.getLongitude()); + zoomMap(location.getLatitude(), location.getLongitude()); findViewById(R.id.LL_Load).setVisibility(View.GONE); } @@ -365,19 +383,6 @@ //LatLng型で受け取っておく currentLatlng = new LatLng(location.getLatitude(), location.getLongitude()); - /* - テスト用。現在座標を画面に表示 - */ -// // 緯度の表示 -// TextView textView1 = findViewById(R.id.text_view1); -// String str1 = "Latitude:"+location.getLatitude(); -// textView1.setText(str1); -// -// // 経度の表示 -// TextView textView2 = findViewById(R.id.text_view2); -// String str2 = "Longitude:"+location.getLongitude(); -// textView2.setText(str2); - //現在地アイコンを表示.このsetIcon内にzoomMap処理もあるので注意 setIcon(location.getLatitude(),location.getLongitude()); } @@ -394,4 +399,4 @@ } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/example/nemophila/ShopCreateActivity.java b/app/src/main/java/com/example/nemophila/ShopCreateActivity.java index e9c82a4..c6d2a90 100644 --- a/app/src/main/java/com/example/nemophila/ShopCreateActivity.java +++ b/app/src/main/java/com/example/nemophila/ShopCreateActivity.java @@ -1,15 +1,20 @@ package com.example.nemophila; import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.ViewModelProvider; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; +import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.example.nemophila.entities.Shop; +import com.example.nemophila.viewmodels.ShopsViewModel; + +import java.io.IOException; public class ShopCreateActivity extends AppCompatActivity {