2017-03-02 78 views
-1

我在Google Maps API中仍然很陌生,我很难将第一次活动的纬度和经度传递给第二次活动。 现在,当我点击第一个活动中的按钮时,它会将经度和纬度值传递给包含谷歌地图的第二个活动。如何将纬度和经度传递给谷歌地图API在android中?

第一项活动:

apotek.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      double lat = -7.94203712; 
    double lng = 112.60936975; 
    Bundle bundle = new Bundle(); 
    Intent intent = new Intent(Menu1.this, menu_cari.class); 
    bundle.putDouble(menu_cari.TAG1,lat); 
    bundle.putDouble(menu_cari.TAG2, lng); 
    startActivity(intent); 
     } 
    }); 

次活动:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (GoogleServicesAvailable()) { 
     Toast.makeText(menu_cari.this, "Terhubung...", Toast.LENGTH_LONG).show(); 
     setContentView(R.layout.activity_menu_cari); 
     initmap(); 
    } else { 
     //maps tidak ditampilkan 
    } 
    Bundle extras = getIntent().getExtras(); 
    lat_apotek=extras.getDouble(TAG1,0); 
    lng_apotek=extras.getDouble(TAG2,0); 
    goToLocation(lat_apotek,lng_apotek,20); 
} 
public void goToLocation(double lat, double lng, float zoom) { 
    LatLng ll = new LatLng(lat, lng); 
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom); 
    mGoogleMap.moveCamera(update); 
} 
+0

是纬度和你在第二个活动正在接受经度为0或不? –

+0

那么你的问题或问题是什么? – rafsanahmad007

+0

@JiteshDalsaniya耶兄弟,它显示0,有时它强制关闭应用程序 – AndriiSG

回答

0

你忘了把演员的捆绑:

Bundle bundle = new Bundle(); 
Intent intent = new Intent(Menu1.this, menu_cari.class); 
bundle.putDouble(menu_cari.TAG1,lat); 
bundle.putDouble(menu_cari.TAG2, lng); 
intent.putExtras(bundle); //add this 

而且ü应该叫goToLocation(lat_apotek,lng_apotek,20);onMapReady方法

同时检查标签:

Bundle extras = getIntent().getExtras(); 
lat_apotek=extras.getDouble(TAG1,0); //TAG1 should match `menu_cari.TAG1` 
+0

它适合我哥们 – AndriiSG

0
apotek.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      double lat = -7.94203712; 
    double lng = 112.60936975; 
    Bundle bundle = new Bundle(); 
    Intent intent = new Intent(Menu1.this, menu_cari.class); 
    bundle.putDouble(menu_cari.TAG1,lat); 
    bundle.putDouble(menu_cari.TAG2, lng); 
    // add this line to put an existing bundle into the intents 
    // you may also conside intent.putExtra() and getIntent().getDouble() in second activity 
    intent.putExtras(bundle); 
    startActivity(intent); 
     } 
    }); 
+0

你应该解释你的答案。 –

0
FirstActivity: 


    apotek.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      double lat = -7.94203712; 
    double lng = 112.60936975; 
    Bundle bundle = new Bundle(); 
    Intent intent = new Intent(Menu1.this, menu_cari.class); 
    bundle.putDouble(menu_cari.TAG1,lat); 
    bundle.putDouble(menu_cari.TAG2, lng); 

/////////////////////////////// 
    intent.putExtra("bundleKey",bundle); 
///////////////////// 

    startActivity(intent); 
     } 
    }); 

对于捆绑访问:

Bundle b = this.getIntent().getExtras(); 
double lng=b.getDouble(key); 
相关问题