2016-04-26 87 views
2

我正嘗試將我的地址通過intent.putExtra()從其他活動傳遞給MapActivity,並在MapActivity之內完成了地理編碼。它沒有錯誤,但是當我運行時,它不顯示位置。任何人都可以告訴我我錯過了什麼或做錯了什麼?無法在Google地圖上顯示多個位置

僅供參考:我已將Listview Activity的地址傳遞給DisplayItem活動(它有效)。現在我想將它傳遞給MapActivity

貝婁是我的其他活動(ListViewActivity

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent intent = new Intent(ShoppingMall.this, Displayitem.class); 
      if (adapter.getItem(position).length > 0) { 

       intent.putExtra(Displayitem.EXTRA_ADDRESS, adapter.getItem(position)[4]); 
       intent.putExtra(MapsActivity.EXTRA_ADDRESS, adapter.getItem(position)[4]); 

      } 

      startActivity(intent); 
     } 
    }); 

我想把

intent.putExtra(MapsActivity.EXTRA_ADDRESS, adapter.getItem(position)[4]); 

這個

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 
    //noinspection SimplifiableIfStatement 
    if (id == R.id.myMap) { 
     Intent intent = new Intent(ShoppingMall.this,MapsActivity.class); 
     //Intent.putExtra 

     startActivity(intent); 
    } 

    return super.onOptionsItemSelected(item); 
} 

這是我的MapActivity

public static final String EXTRA_ADDRESS ="address"; 
private Bundle extras; 
List<Address> location; 
double lati, lon ; 
. 
. 
. 
. 


private void getAddress(GoogleMap googleMap) { 

    try { 
     mMap = googleMap; 

     Intent intent = getIntent(); 
     extras = intent.getExtras(); 
     Geocoder geocoder = new Geocoder(this); 
     String address = extras.getString(this.EXTRA_ADDRESS); 
     location = geocoder.getFromLocationName(address, 1); 
     if (location.size() > 0) { 
      lati =location.get(0).getLatitude(); 
      lon =location.get(0).getLongitude(); 
      MarkerOptions options = new MarkerOptions() 
        .title("") 
        .position (new LatLng(lati, lon)); 
      mMap.addMarker(options); 
     } 

    }catch (Exception e){ 
     e.printStackTrace(); 

    } 
} 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    getAddress(googleMap); 
    setUpMap(); 

} 

PS:新手在android系統

回答

0

試試這個....

Intent intent = new Intent(ShoppingMall.this, Mapactivity.class); 
      if (adapter.getItem(position).length > 0) { 

       intent.putExtra(Displayitem.EXTRA_ADDRESS, adapter.getItem(position)[4]); 
       intent.putExtra(MapsActivity.EXTRA_ADDRESS, adapter.getItem(position)[4]); 

      } 

      startActivity(intent); 
+0

它在地圖上顯示。我可以製作單獨的課程嗎?像 Intent intent = new Intent(ShoppingMall.this,Mapactivity.class); and Intent intent = new Intent(ShoppingMall.this,Displayitem.class); – Critako

+0

是的上面寫着MapActivity代碼和意圖傳遞displayitem。所以你可以創建兩個單獨的課程。 –

+0

惠,我意識到這個問題,我已經更新了我的問題。希望你能理解 – Critako

0
private void addMarkersToMap() { 
    mMap.clear(); 
    for (int i = 0; i < private void addMarkersToMap() { 
mMap.clear(); 
for (int i = 0; i < location.size(); i++) {   
     LatLng ll = new LatLng(location.get(i).getPos().getLat(), location.get(i).getPos().getLon()); 
     BitmapDescriptor bitmapMarker; 
     switch (location.get(i).getState()) { 
     case 0: 
      bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED); 
      Log.i(TAG, "RED"); 
      break; 
     case 1: 
      bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN); 
      Log.i(TAG, "GREEN"); 
      break; 
     case 2: 
      bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE); 
      Log.i(TAG, "ORANGE"); 
      break; 
     default: 
      bitmapMarker = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED); 
      Log.i(TAG, "DEFAULT"); 
      break; 
     }    
     mMarkers.add(mMap.addMarker(new MarkerOptions().position(ll).title(location.get(i).getName()) 
       .snippet(getStateString(location.get(i).getState())).icon(bitmapMarker))); 

     Log.i(TAG,"Car number "+i+" was added " +mMarkers.get(mMarkers.size()-1).getId()); 
    } 
}