gpt4 book ai didi

java - 如何在android中的onMapReady()中获取onNavigationItemSelected()值

转载 作者:行者123 更新时间:2023-12-01 07:48:49 25 4
gpt4 key购买 nike

我试图为抽屉导航中列出的每个项目提供位置坐标,并将其指向谷歌地图。

它如何获取我分配给 onNavigationItemSelected() 到 onMapReady() 中的变量的纬度和经度?

//This is were I assign the coordiantes
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

double lat = 0;
double lon = 0;

if (id == R.id.redBuilding){
/*lat = 13.010982;
lon = 80.235444;

String latitude = Double.toString(lat);
String longitude = Double.toString(lon);
latLong = latitude+"/"+longitude; */
}

if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

我想在此方法中获取分配的坐标以将其指向 map 上:

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng latLon = new LatLng(13.0827, 80.2707);
mMap.addMarker(new MarkerOptions().position(latLon).title("Marker is in Chennai.").snippet("India"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLon));
}

我只使用一个 Activity,其中 map 位于 Fragment 中。

最佳答案

如果您想更改标记,则不必使用onMapReady。您可以在初始化 map 时保存标记,然后在每次用户选择项目时更改它。

private Marker marker;

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng latLon = new LatLng(13.0827, 80.2707);
marker = mMap.addMarker(new MarkerOptions().position(latLon).title("Marker is in Chennai.").snippet("India"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLon));
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

double lat = 0;
double lon = 0;

if (id == R.id.redBuilding){
lat = 13.010982;
lon = 80.235444;

mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon));
marker.setPosition(new LatLng(lat, lon));
marker.setTitle("your new title");
}

if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

关于java - 如何在android中的onMapReady()中获取onNavigationItemSelected()值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com