我正在尝试为导航抽屉中列出的每个项目提供位置坐标,并将其指向Google地图。
如何将我分配给onNavigationItemSelected()中的变量的lat和long提取到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;
}
我想在此方法中获取指定的坐标以将其指向地图:
@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));
}
我只使用一个活动,地图位于片段中。
答案 0 :(得分:3)
如果您想要更改标记,则不必使用onMapReady
。您可以在初始化地图时保存标记,然后在每次用户选择项目时更改它。
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;
}
答案 1 :(得分:2)
@Guillaume:
导致运行时异常:
java.lang.NullPointerException
at mca.annauniv.ceg.MainActivity.a(Unknown Source)
at android.support.design.widget.NavigationView$1.a(Unknown Source)
at android.support.v7.view.menu.h.a(Unknown Source)
at android.support.v7.view.menu.u.a(Unknown Source)
at android.support.v7.view.menu.j.b(Unknown Source)
at android.support.v7.view.menu.h.a(Unknown Source)
at android.support.design.internal.c$1.onClick(Unknown Source)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 2 :(得分:1)
如果onNavigationItemSelected()和onMapReady()方法都在同一个活动中,那么您可以通过使变量全局变量轻松获取值。如果他们处于不同的活动,那么你可以使用模型或意图。