2013-07-02 26 views
0

我在遇到以下問題: 我在Google地圖上有多個標記。我使用For循環完成了這一過程,該循環遍歷我的對象數組,併爲每個對象添加一個標記。標記顯示標題和地址。但是現在我需要製作可點擊的InfoWindow(或者只是可點擊的標記),它將顯示包含附加信息(描述)的AlertDialog。但我無法得到這個工作。或者,數據不需要顯示在AlertDialog中,我也可以在TextView中顯示它。AlertDialog在谷歌地圖上有多個標記

這裏的顯示標記部分的代碼(這是一個FragmentActivity):

... 

Double longitude, latitude; 
static LatLng coordinates; 
GoogleMap supportMap; 
String title, address; 
BitmapDescriptor bdf; 
ArrayList<GasStations> listGas = new ArrayList<GasStations>(); 

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    supportMap = fm.getMap(); 
... 

if (listGas != null) { 
     for (int i = 0; i < listGas.size(); i++) { 
      longitude = listGas.get(i).getLongitude(); 
      latitude = listGas.get(i).getLatitude(); 
      naslov = listGas.get(i).getTitle(); 
      adresa = listGas.get(i).getAddress() + " " 
        + listGas.get(i).getLocation(); 
      koordinate = new LatLng(latitude, longitude); 

      supportMap.addMarker(new MarkerOptions().position(koordinate) 
        .title(title).snippet(address).icon(bdf)); 
      supportMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
        coordinates, 10)); 
     } 
    } 

標記顯示就好了他們的信息窗口顯示正確的數據。但是現在我想顯示基於哪個InfoWindow被點擊的附加信息。如果這不能通過InfoWindow完成,可以通過點擊一個特定的標記來完成嗎?

回答

1

您可以創建自定義信息窗口

GoogleMap googleMap; 

    Map.setInfoWindowAdapter(new InfoWindowAdapter() { 


     @Override 
     public View getInfoWindow(Marker arg0) { 
      return null; 
     } 


     @Override 
     public View getInfoContents(Marker arg0) { 

      // Getting view from the layout file custom_window 
      View v = getLayoutInflater().inflate(R.layout.custom_window, null); 

      // Getting the position from the marker 
      LatLng latLng = arg0.getPosition(); 


      TextView tvLat = (TextView) v.findViewById(R.id.lat); 


      TextView tvLng = (TextView) v.findViewById(R.id.lng); 

      tvLat.setText("Lat:" + latLng.latitude); 


      return v; 

     } 
    }); 
+0

我會嘗試這種玩弄周圍,看看我可以讓它顯示我需要的東西。 –

-1

公共類MainActivity擴展FragmentActivity {

GoogleMap googleMap; 
String add; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    googleMap = mapFragment.getMap(); 
    googleMap.setInfoWindowAdapter(new InfoWindowAdapter() { 

     @Override 
     public View getInfoWindow(Marker arg0) { 
      return null; 
     } 

     @Override 
     public View getInfoContents(Marker arg0) { 

      View v = getLayoutInflater().inflate(R.layout.info_window_layout, null); 
      LatLng latLng = arg0.getPosition(); 
      TextView tvLat = (TextView) v.findViewById(R.id.tv_lat); 
      TextView tvLng = (TextView) v.findViewById(R.id.tv_lng); 
      TextView loc = (TextView) v.findViewById(R.id.loc); 
      Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault()); 
      try {   
       List<Address> addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); 
       if(addresses.size() > 0) 
       add = addresses.get(0).getAddressLine(0); 
      } 

      catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      loc.setText(""+add); 
      tvLat.setText("" + latLng.latitude); 
      tvLng.setText(""+ latLng.longitude); 

      return v; 

     } 
    }); 

    googleMap.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng arg0) { 
      googleMap.clear(); 

      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.position(arg0); 
      googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0)); 

      Marker marker = googleMap.addMarker(markerOptions); 
      marker.showInfoWindow(); 

     } 
    }); 

    googleMap.setOnInfoWindowClickListener(
      new OnInfoWindowClickListener() { 

       @Override 
       public void onInfoWindowClick(Marker marker) { 
        Toast.makeText(getBaseContext(), "Info Window [email protected]" + marker.getId(), 
          Toast.LENGTH_SHORT).show(); 
       } 
      }); 

} 

@Override 
protected void onResume() { 
super.onResume(); 

int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
if (resultCode == ConnectionResult.SUCCESS) {  
    Toast.makeText(getApplicationContext(), "isGooglePlayServicesAvailable SUCCESS", Toast.LENGTH_SHORT).show(); 
} 
else {  
    GooglePlayServicesUtil.getErrorDialog(resultCode, this, 1); 
    Toast.makeText(getApplicationContext(), "isGooglePlayServicesAvailable ERROR", Toast.LENGTH_SHORT).show(); 
} 

}

}