diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f55a4e3..ef76412 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -80,7 +80,7 @@ - + { - Log.d("debug", "myPageButton, マイページに画面遷移"); - Intent intent = new Intent(MainActivity.this, MyPageActivity.class); - startActivity(intent); - }); +// +// nemophila = (Nemophila) this.getApplication(); +// +// ImageButton myPageButton = findViewById(R.id.myPageButton); +// // lambda式 +// myPageButton.setOnClickListener( v -> { +// Log.d("debug", "myPageButton, マイページに画面遷移"); +// Intent intent = new Intent(MainActivity.this, MyPageActivity.class); +// startActivity(intent); +// }); if (ActivityCompat.checkSelfPermission(this, diff --git a/app/src/main/java/com/example/nemophila/MapsActivity.java b/app/src/main/java/com/example/nemophila/MapsActivity.java index 0147d71..e760779 100644 --- a/app/src/main/java/com/example/nemophila/MapsActivity.java +++ b/app/src/main/java/com/example/nemophila/MapsActivity.java @@ -1,5 +1,6 @@ package com.example.nemophila; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.drawerlayout.widget.DrawerLayout; @@ -8,7 +9,10 @@ import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; +import android.content.Intent; import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; public class MapsActivity extends AppCompatActivity { @@ -39,4 +43,19 @@ // ナビゲーションUIをセットアップする NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration); } + + @Override + public boolean onCreateOptionsMenu(@NonNull Menu menu) { + getMenuInflater().inflate(R.menu.tool_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + Intent intent = new Intent(MapsActivity.this, MyPageActivity.class); + startActivity(intent); + + return super.onOptionsItemSelected(item); + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/nemophila/MapsFragment.java b/app/src/main/java/com/example/nemophila/MapsFragment.java index 16aa92e..c9db8c8 100644 --- a/app/src/main/java/com/example/nemophila/MapsFragment.java +++ b/app/src/main/java/com/example/nemophila/MapsFragment.java @@ -1,30 +1,185 @@ package com.example.nemophila; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; +import android.Manifest; +import android.annotation.SuppressLint; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.location.Location; +import android.location.LocationListener; +import android.location.LocationManager; import android.os.Bundle; +import android.provider.Settings; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.Toast; +import com.example.nemophila.databinding.ActivityMainBinding; +import com.example.nemophila.entities.Shop; +import com.example.nemophila.viewmodels.ShopsViewModel; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; +import com.google.android.gms.maps.model.BitmapDescriptor; +import com.google.android.gms.maps.model.BitmapDescriptorFactory; +import com.google.android.gms.maps.model.CameraPosition; +import com.google.android.gms.maps.model.GroundOverlay; +import com.google.android.gms.maps.model.GroundOverlayOptions; import com.google.android.gms.maps.model.LatLng; +import com.google.android.gms.maps.model.LatLngBounds; +import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; +import com.google.common.collect.Maps; -public class MapsFragment extends Fragment { +public class MapsFragment extends Fragment implements LocationListener { + ShopsViewModel shopsViewModel; + + Nemophila nemophila; + private GoogleMap mMap; + private ActivityMainBinding binding; + private LatLng currentLatlng = null; + private LatLng initialLatlng; + private LatLng shopLatlng; + private LatLng testLatlng; + private LatLng testLatlng2; + private LatLng tapLatlng; + private CameraPosition nowCamera; + private LatLng nowLatlng; + + private GroundOverlay currentOverlay; + LocationManager locationManager; private OnMapReadyCallback callback = new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap googleMap) { - LatLng sydney = new LatLng(-34, 151); - googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); - googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); + + nemophila = (Nemophila) getActivity().getApplication(); + mMap = googleMap; + + + //ViewModelへのアクセス + shopsViewModel = new ViewModelProvider(getActivity()).get(ShopsViewModel.class); + + // LiveDataへの購読 + shopsViewModel.getShopsLiveData().observe(getActivity(), shops -> { + //受け取ったshopsに対してMarkerが立っているかを確認 + //対応する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("")); + //マーカーに店情報を持たせる + createMaker.setTag(shop); + //ShopToMarkerに紐づけ + shopsViewModel.setShopAndMarker(shop, createMaker); + } + } + + }); + +// binding = ActivityMainBinding.inflate(getLayoutInflater()); +// getActivity().setContentView(binding.getRoot()); +// +// if (ActivityCompat.checkSelfPermission(getContext(), +// Manifest.permission.ACCESS_FINE_LOCATION) +// != PackageManager.PERMISSION_GRANTED) { +// +// requestPermissionLauncher.launch( +// Manifest.permission.ACCESS_FINE_LOCATION); +// } +// else{ +// locationStart(); +// } + + // Obtain the SupportMapFragment and get notified when the map is ready to be used. +// SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager() +// .findFragmentById(R.id.map); +// assert mapFragment != null; +// mapFragment.getMapAsync(this); + + //初期画面の座標(現在地をロードするまで表示) + //initialLatlng = new LatLng(39,138); + initialLatlng = new LatLng(nemophila.getCameraLatitude(), nemophila.getCameraLongitude()); + //初期画面に移動 + //mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatlng, 15f)); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(initialLatlng, nemophila.getZoom())); + + + //画面が動いたとき + mMap.setOnCameraIdleListener(() -> { + //カメラの座標とZOOM倍率を保存 + //nowCamera = mMap.getCameraPosition(); + nemophila.setCameraLatitude(mMap.getCameraPosition().target.latitude); + nemophila.setCameraLongitude(mMap.getCameraPosition().target.longitude); + nemophila.setZoom(mMap.getCameraPosition().zoom); + + //nowLatlng = new LatLng(nowCamera.target.latitude, nowCamera.target.longitude); + //nowZoom = nowCamera.zoom; + //Shopの描画範囲を指定 + shopsViewModel.setViewArea(nemophila.getCameraLongitude() + 1, nemophila.getCameraLatitude() + 1, nemophila.getCameraLongitude() - 1, nemophila.getCameraLatitude() - 1); + }); + + /* + //多分使わない + // タップした時のリスナーをセット + mMap.setOnMapClickListener(tapLocation -> { + // map(ピン以外)をtapされた位置の緯度経度 + 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 -> { + //長押しされた位置の緯度経度を取得 + //LatLng newlocation = new LatLng(longpushLocation.latitude, longpushLocation.longitude); + //Nemophilaに座標を保存 + nemophila.setCurrentLatitude(longpushLocation.latitude); + nemophila.setCurrentLongitude(longpushLocation.longitude); + //長押しした場合は今からShopを作成するので、CurrentShopをnullで登録しておく + nemophila.setCurrentShop(null); + //ShopCreate画面に遷移 + Intent intent = new Intent(getActivity(), ShopCreateActivity.class); + startActivity(intent); + }); + + // ピンをクリックした場合 + mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() { + @Override + public boolean onMarkerClick(Marker marker) { + //店の詳細と投稿一覧を表示 + //ここでshopActivityを呼び出す + + //下からクリックしたことを通知 + Toast.makeText(getActivity(), "ピンクリック", Toast.LENGTH_SHORT).show(); + //選んだ店をsetする(現状はnullになっているが後で直す) + nemophila.setCurrentShop((Shop) marker.getTag()); + System.out.println(nemophila.getCurrentShop().getName()); + + //ShopActivity画面に遷移 + Intent intent = new Intent(getActivity(), ShopActivity.class); + startActivity(intent); + + return false; + } + }); } }; @@ -34,16 +189,193 @@ @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_maps, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + + //binding = ActivityMainBinding.inflate(getLayoutInflater()); + //getActivity().setContentView(binding.getRoot()); + + if (ActivityCompat.checkSelfPermission( + getContext(), + Manifest.permission.ACCESS_FINE_LOCATION) + != PackageManager.PERMISSION_GRANTED) { + + requestPermissionLauncher.launch( + Manifest.permission.ACCESS_FINE_LOCATION); + } + else{ + locationStart(); + } + +// // Obtain the SupportMapFragment and get notified when the map is ready to be used. +// SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() +// .findFragmentById(R.id.map); +// assert mapFragment != null; +// mapFragment.getMapAsync(this); + + SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); if (mapFragment != null) { mapFragment.getMapAsync(callback); } } + + + private void zoomMap(double latitude, double longitude) { + // 表示する東西南北の緯度経度を設定 + double south = latitude * (1 - 0.00005); + double west = longitude * (1 - 0.00005); + double north = latitude * (1 + 0.00005); + double east = longitude * (1 + 0.00005); + + LatLng latlng = new LatLng(latitude, longitude); + + // LatLngBounds (LatLng southwest, LatLng northeast) + //左下、右上 + LatLngBounds bounds = LatLngBounds.builder() + .include(new LatLng(south, west)) + .include(new LatLng(north, east)) + .build(); + + int width = getResources().getDisplayMetrics().widthPixels; + int height = getResources().getDisplayMetrics().heightPixels; + + // static CameraUpdate.newLatLngBounds(LatLngBounds bounds, int width, int height, int padding) + mMap.moveCamera(CameraUpdateFactory. + newLatLngBounds(bounds, width, height, 0)); + + //ズーム処理 + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, nemophila.getZoom())); + } + + //現在地の表示 + private void setIcon(double latitude, double longitude) { + //Drawable ic_current = ResourcesCompat.getDrawable(getResources(),R.drawable.icon_current,null); + //更新前の現在地アイコンを消去 + if (currentOverlay != null) { + currentOverlay.remove(); + } + + LatLng current_location = new LatLng(latitude, longitude); + + // マップに貼り付ける BitmapDescriptor生成 + BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_current); + //Drawable ic_current = ResourcesCompat.getDrawable(getResources(),R.drawable.ic_current_location,null); + + // 貼り付設定 + GroundOverlayOptions overlayOptions = new GroundOverlayOptions(); + overlayOptions.image(descriptor); + + //public GroundOverlayOptions anchor (float u, float v) + // (0,0):top-left, (0,1):bottom-left, (1,0):top-right, (1,1):bottom-right + overlayOptions.anchor(0.5f, 0.5f); + + // 張り付け画像の大きさ メートル単位 + // public GroundOverlayOptions position(LatLng location, float width, float height) + overlayOptions.position(current_location, 200f, 200f); + + // マップに貼り付け・アルファを設定 + currentOverlay = mMap.addGroundOverlay(overlayOptions); + + // 透明度 + assert currentOverlay != null; + currentOverlay.setTransparency(0.8F); + } + + + private final ActivityResultLauncher + requestPermissionLauncher = registerForActivityResult( + new ActivityResultContracts.RequestPermission(), + isGranted -> { + if (isGranted) { + locationStart(); + } + else { + Toast toast = Toast.makeText(getActivity(), + "これ以上なにもできません", Toast.LENGTH_SHORT); + toast.show(); + } + }); + + //現在地の取得 + @SuppressLint("MissingPermission") + private void locationStart(){ + Log.d("debug","locationStart()"); + + // LocationManager インスタンス生成 + locationManager = + (LocationManager) getContext().getSystemService(getContext().LOCATION_SERVICE); + + if (locationManager != null && locationManager.isProviderEnabled( + LocationManager.GPS_PROVIDER)) { + + Log.d("debug", "location manager Enabled"); + } else { + // GPSを設定するように促す + Intent settingsIntent = + new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + startActivity(settingsIntent); + Log.d("debug", "not gpsEnable, startActivity"); + } + + if (ContextCompat.checkSelfPermission(getContext(), + Manifest.permission.ACCESS_FINE_LOCATION) != + PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(getActivity(), + new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 1000); + + Log.d("debug", "checkSelfPermission false"); + return; + } + + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, + 1000, 50, this); + + } + + @Override + public void onLocationChanged(Location location) { + //初期画面は現在地を中心にするため... + if (currentLatlng == null){ + //↓現在地ロード後画面中心を現在地にする場合 + //zoomMap(location.getLatitude(), location.getLongitude()); + //ロード画面の終了 + //getView().findViewById(R.id.LL_Load).setVisibility(View.GONE); + + //現在地ボタンを表示 + getView().findViewById(R.id.currentButton).setVisibility(View.VISIBLE); + ImageButton button1 = getView().findViewById(R.id.currentButton); + + button1.setOnClickListener( v -> { + Log.d("debug", "currentbutton, 現在地にカメラを移動"); + System.out.println(shopsViewModel.getShopsLiveData().getValue()); + //現在地にカメラを移動 + zoomMap(currentLatlng.latitude, currentLatlng.longitude); + }); + } + + //現在地が変更されるたびに現在地アイコンを移動 + //LatLng型で受け取っておく + currentLatlng = new LatLng(location.getLatitude(), location.getLongitude()); + + //現在地アイコンを表示.このsetIcon内にzoomMap処理もあるので注意 + setIcon(location.getLatitude(),location.getLongitude()); + } + + @Override + public void onProviderEnabled(String provider) { + + } + + @Override + public void onProviderDisabled(String provider) { + + } + } \ No newline at end of file diff --git a/app/src/main/java/com/example/nemophila/MyPageActivity.java b/app/src/main/java/com/example/nemophila/MyPageActivity.java index ae30c66..0791148 100644 --- a/app/src/main/java/com/example/nemophila/MyPageActivity.java +++ b/app/src/main/java/com/example/nemophila/MyPageActivity.java @@ -40,7 +40,7 @@ Button cancelButton = (Button) findViewById(R.id.cancel); cancelButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { - Intent intent = new Intent(MyPageActivity.this, MainActivity.class); + Intent intent = new Intent(MyPageActivity.this, MapsActivity.class); startActivity(intent); } }); diff --git a/app/src/main/res/layout/activity_maps.xml b/app/src/main/res/layout/activity_maps.xml index 05b3440..500e992 100644 --- a/app/src/main/res/layout/activity_maps.xml +++ b/app/src/main/res/layout/activity_maps.xml @@ -21,22 +21,56 @@ app:defaultNavHost="true" app:navGraph="@navigation/navigation" /> + + + + + + + + + + android:background="#00000000" + app:menu="@menu/tool_menu"> - + + + + + android:layout_height="match_parent"> - + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_maps.xml b/app/src/main/res/layout/fragment_maps.xml index 59319fb..ad904de 100644 --- a/app/src/main/res/layout/fragment_maps.xml +++ b/app/src/main/res/layout/fragment_maps.xml @@ -1,14 +1,53 @@ - + - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/tool_menu.xml b/app/src/main/res/menu/tool_menu.xml new file mode 100644 index 0000000..9b42751 --- /dev/null +++ b/app/src/main/res/menu/tool_menu.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file