2017-04-13 41 views
0

我試圖給出導航抽屜中列出的每個項目的位置座標,並將它指向谷歌地圖。如何在android中的onMapReady()中獲取onNavigationItemSelected()值

它如何獲取我分配給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; 
} 

我想獲取這個方法分配的座標點在地圖上:

@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

是'中的片段的方法'onMapReady(使用GoogleMap使用GoogleMap)? – CodeCameo

+0

你正在加載地圖在不同的片段?或者在活動中加載地圖片段 – tahsinRupam

+0

@SifatOshan,no。 –

回答

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

如果兩個onNavigationItemSelected()和onMapReady()方法是在同一個活動,然後你可以通過可變全球輕易獲取的值。如果他們在不同的活動,那麼你可以使用模型或意圖。

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) 
+0

您何時有此錯誤?當你選擇你的redBuilding項目?如果是,請確保您的地圖已初始化(地圖顯示標記爲「Marker在Chennai」。) – Guillaume

+0

謝謝@Guillaume。有用。謝謝大家的傑出貢獻。 –

相關問題