作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图为抽屉导航中列出的每个项目提供位置坐标,并将其指向谷歌地图。
它如何获取我分配给 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/
我是一名优秀的程序员,十分优秀!