2017-03-02 16 views
-1

私はまだGoogle Maps APIで新しく、最初のアクティビティから2番目のアクティビティまで緯度と経度を渡すことが難しいです。 最初のアクティビティでボタンをクリックすると、緯度と経度の値がGoogleマップを含む2番目のアクティビティに渡されます。アンドロイドのGoogleマップAPIに緯度と経度を渡す方法は?

最初のアクティビティ:

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

は緯度であり、2番目のアクティビティで受信している経度は0又はありませんか? –

+0

なので質問や問題は何ですか? – rafsanahmad007

+0

@JiteshDalsaniya yeah bro、それは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 

またuはでgoToLocation(lat_apotek,lng_apotek,20);を呼び出す必要がありますバンドルアクセスについて

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); 
     } 
    }); 

:3210方法

もタグをチェック

Bundle b = this.getIntent().getExtras(); 
double lng=b.getDouble(key); 
関連する問題