main画面の構築
ピンを初期画面で2つに変更
地図を長押し時にピンを立てる処理を追加
ピンを押した時の処理を追加
1 parent 55fa67b commit dcabf15b2571fe53d1425e44e3094130ae0d426d
r-nishimura authored on 6 Jun 2023
Showing 1 changed file
View
340
app/src/main/java/com/example/nemophila/MainActivity.java
package com.example.nemophila;
 
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
 
import android.os.Bundle;
 
import android.widget.Toast;
 
import com.google.android.gms.maps.CameraUpdate;
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.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.example.nemophila.databinding.ActivityMainBinding;
 
import java.util.Locale;
 
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
 
private GoogleMap mMap;
private ActivityMainBinding binding;
 
private LatLng latlng;
private LatLng latlng2;
private LatLng location;
 
//ひとまずここは触らない
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
 
// 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);
}
 
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
/*
ここからマップに関する操作
*/
/*
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
 
// Add a marker in Sydney and move the
//マーカー(ピン)の登録・表示方法(例)
LatLng ntlab = new LatLng(34.73, 135.26);
mMap.addMarker(new MarkerOptions().position(ntlab).title("Marker in ntlab"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(ntlab));
}
 
//カメラの移動処理方法(例)
CameraUpdate cUpdate = CameraUpdateFactory.newLatLngZoom(
new LatLng(34.73, 135.26), 15);
mMap.moveCamera(cUpdate);
}
*/
 
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
 
//研究室周辺の緯度経度
double latitude = 34.735;
double longitude = 135.26;
 
latlng = new LatLng(latitude, longitude);
//テスト用
latlng2 = new LatLng(34.74, 135.26);
 
// 標準のマーカー
setMarker(latitude, longitude);
 
// アイコン画像をマーカーに設定
//画像関連がわかっていないのでパス
//setIcon(latitude, longitude);
 
// camera 移動 (初期画面)
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 15));
 
/*
//多分使わない
// タップした時のリスナーをセット
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));
});
*/
 
//長押し時にその座標にピンを立てる
//長押し時にその座標を保存し、投稿画面に移り、投稿完了時にはピンを立てる
mMap.setOnMapLongClickListener(longpushLocation -> {
//長押しされた位置の緯度経度を取得
LatLng newlocation = new LatLng(longpushLocation.latitude, longpushLocation.longitude);
 
//投稿処理
 
 
//投稿が完了した場合
//ピンをその座標に立て、緯度経度をタイトルに設定
//タイトルはnullにする予定
mMap.addMarker(new MarkerOptions().position(newlocation).title(""+longpushLocation.latitude+" :"+ longpushLocation.longitude));
//ピンを立てた位置にカメラを移動
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newlocation, 18));
});
 
// ピンをクリックした場合
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//店の詳細と投稿一覧を表示
//ここでshopActivityを呼び出す
 
//下からクリックしたことを通知
Toast.makeText(MainActivity.this, "ピンクリック", Toast.LENGTH_SHORT).show();
return false;
}
});
 
}
 
//ピンの設定
private void setMarker(double latitude, double longitude){
 
MarkerOptions markerOptions = new MarkerOptions();
MarkerOptions markerOptions2 = new MarkerOptions();
 
markerOptions.position(latlng);
markerOptions.title("ntlab");
mMap.addMarker(markerOptions);
 
//テスト用
markerOptions2.position(latlng2);
markerOptions2.title(null);
mMap.addMarker(markerOptions2);
 
// ズーム
zoomMap(latitude, longitude);
 
}
 
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);
 
// 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, 15f));
}
 
// private void setIcon(double latitude, double longitude){
//
// // マップに貼り付ける BitmapDescriptor生成
// // 画像は自分で適当に用意します。ここではmipmapから持ってきましたが
// BitmapDescriptor descriptor =
// BitmapDescriptorFactory.fromResource(R.drawable.droid_round);
//
// // 貼り付設定
// 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(latlng, 300f, 300f);
//
// // マップに貼り付け・アルファを設定
// GroundOverlay overlay = mMap.addGroundOverlay(overlayOptions);
// // ズーム
// zoomMap(latitude, longitude);
//
// // 透明度
// assert overlay != null;
// overlay.setTransparency(0.0F);
// }
}