0

我正在处理地图应用程序,我通过自定义对象POJO类的ArrayList的纬度和经度位置添加标记。一切工作正常,我可以添加标记的位置,当我尝试从ArrayList中获取标记索引位置onMarkerClick(即我通过marker.getPosition()只获得标记纬度/经度位置)时出现问题。但我的要求是当我点击特定标记时从我的ArrayList中获取确切的索引位置。从ArrayList中获取标记索引位置<Object> onMarkerClick方法

我发布我的完整MapActivity代码供您参考。

public class MainActivity extends FragmentActivity implements GoogleMap.OnMarkerClickListener { 

    GoogleMap googleMap;  
    private ArrayList<LocationModel> mLocationList = new ArrayList<LocationModel>(); 

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


     // Getting Google Play availability status 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 

     // Showing status 
     if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available 

      int requestCode = 10; 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 

     }else { // Google Play Services are available   

      // Getting reference to the SupportMapFragment of activity_main.xml 
      SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

      // Getting GoogleMap object from the fragment 
      googleMap = fm.getMap(); 

      // Changing map type 
      googleMap.setMapType (GoogleMap.MAP_TYPE_NORMAL); 

      googleMap.setOnMarkerClickListener(this); 
      // Enabling MyLocation Layer of Google Map 
      googleMap.setMyLocationEnabled(true); 

      mLocationList.add(new LocationModel ("11.0183", "76.9725", "Hotel Meridean")); 
      mLocationList.add(new LocationModel ("13.1073", "80.2445","Hotel Lee palace")); 
      mLocationList.add(new LocationModel ("12.9854", "80.1398","Hill view")); 
      mLocationList.add(new LocationModel ("12.9750", "80.1347","Hotel Radisson")); 
      mLocationList.add(new LocationModel ("11.6500", "78.1600","Hotel Mahabarath")); 


       // Iterating through all the locations stored 
       for(int i=0;i<mLocationList.size();i++){ 


        drawMarker (new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude()), Double.parseDouble (mLocationList.get (i).getLongitude()))); 


       } 


      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target (new LatLng (Double.parseDouble ("13.0900"), Double.parseDouble ("80.2700")))// Sets the center of the map to tamil nadu 
        .zoom (7)     // Sets the zoom 
//     .bearing(30)    // Sets the orientation of the camera to east 
//     .tilt(30)     // Sets the tilt of the camera to 30 degrees 
        .build();     // Creates a CameraPosition from the builder 


      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), new GoogleMap.CancelableCallback() { 

       @Override 
       public void onFinish() { 
        CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(-300, 70); 
        googleMap.animateCamera(cu_scroll); 
       } 

       @Override 
       public void onCancel() { 
       } 
      }); 
     }   

     googleMap.setOnMapClickListener(new OnMapClickListener() { 

      @Override 
      public void onMapClick(LatLng point) {  

//    Toast.makeText(getBaseContext(), ""+point, Toast.LENGTH_SHORT).show(); 

      } 
     }); 


     googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {    
      @Override 
      public void onMapLongClick(LatLng point) { 
      } 
     });   


    } 

    private void drawMarker(LatLng point){ 
     // Creating an instance of MarkerOptions 
     MarkerOptions markerOptions = new MarkerOptions();     

     // Setting latitude and longitude for the marker 
     markerOptions.position(point); 

     // Adding marker on the Google Map 
     googleMap.addMarker(markerOptions); 
    } 


    @Override 
    public boolean onMarkerClick (Marker marker) { 

     Util.showToast (MainActivity.this,""+marker.getPosition()); 

     return false; 
    } 
} 

我LocationModel POJO类是提前

public class LocationModel { 

    private String latitude; 

    private String longitude; 

    private String hotelName; 

    public String getLatitude() { 
     return latitude; 
    } 

    public void setLatitude (String latitude) { 
     this.latitude = latitude; 
    } 

    public String getLongitude() { 
     return longitude; 
    } 

    public void setLongitude (String longitude) { 
     this.longitude = longitude; 
    } 

    public LocationModel (String latitude, String longitude, String hotelName) { 
     this.latitude = latitude; 
     this.longitude = longitude; 
     this.hotelName = hotelName; 
    } 

    public String getHotelName() { 
     return hotelName; 
    } 

    public void setHotelName (String hotelName) { 
     this.hotelName = hotelName; 
    } 
} 

感谢。请通过宝贵的建议和答案帮助我解决问题。

回答

3

试试这个

定义

private Map<Integer, LatLng> marker = new HashMap(); 

然后

for(int i=0;i<mLocationList.size();i++){ 

drawMarker (new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude()), Double.parseDouble (mLocationList.get (i).getLongitude()))); 

marker.put(i,new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude()), Double.parseDouble (mLocationList.get (i).getLongitude()))); 
} 

然后

googleMap.setOnMapClickListener(new OnMapClickListener() { 

      @Override 
      public void onMapClick(LatLng point) {  

      // iterate your map and get the index by your point 

       Integer value = map.get(point); 

      } 
     }); 
+0

感谢@Soham。如何基于点迭代地图。如果我们在地图上反转键和值,该怎么办? – Chandru

+0

private Map marker = new HashMap(); – Chandru

+0

是的,你可以扭转地图中的值,这将是罚款@Chandru – Soham

0

你可以使用HashMap中,而不是ArrayList中,和HashMap的关键将是这个类:

public class LocationModelKey { 

private String latitude; 

private String longitude; 

@Override 
public boolean equals(Object o) { 
    if (this == o) return true; 
    if (o == null || getClass() != o.getClass()) return false; 

    LocationModelKey that = (LocationModelKey) o; 

    if (latitude != null ? !latitude.equals(that.latitude) : that.latitude != null) 
     return false; 
    return !(longitude != null ? !longitude.equals(that.longitude) : that.longitude != null); 

} 

@Override 
public int hashCode() { 
    int result = latitude != null ? latitude.hashCode() : 0; 
    result = 31 * result + (longitude != null ? longitude.hashCode() : 0); 
    return result; 
}} 

然后就是获得通过特定的按键对象(新LocationModelKey(“你的纬度”,“你长”) ;

1

请检查下面的变化,同时加入了标记组索引标题,在这里指数是你我的价值。

private void drawMarker(LatLng point , int index){ 
    // Creating an instance of MarkerOptions 
    MarkerOptions markerOptions = new MarkerOptions();     

    // Setting latitude and longitude for the marker 
    markerOptions.position(point).title(""+index); 

    // Adding marker on the Google Map 
    googleMap.addMarker(markerOptions); 
} 

和标记单击

 googleMap.setOnMarkerClickListener(new OnMarkerClickListener() { 

     @Override 
     public boolean onMarkerClick(Marker marker) { 

      int index = Integer.valueOf(marker.getTitle()); 
      return true; 
     } 
    }); 
+0

感谢您的解决方案,但标记标题工具提示正在显示,同时点击标记 – Chandru

+0

@Chandru从onMarkerClick方法返回true – mdDroid

1

你可以地图Marker添加到Integer甚至的Marker地图LocationModel取决于你想要做什么。但看到MarkerInteger真正回答你的问题,你可以这样做:

private Map<Marker, Integer> mMarkerMap = new HashMap<Marker, Integer>(); 

然后做一个小的变化,以您的drawMarker()方法,增加的位置在ArrayList作为参数,并保存新创建的标记在Map

private void drawMarker(LatLng point, int positionInLocationList){ 
    // Creating an instance of MarkerOptions 
    MarkerOptions markerOptions = new MarkerOptions();     

    // Setting latitude and longitude for the marker 
    markerOptions.position(point); 

    // Adding marker on the Google Map 
    Marker marker = googleMap.addMarker(markerOptions); 

    mMarkerMap.put(marker, positionInLocationList); 
} 

然后,在你打电话drawMarker()你只是索引添加到呼叫:

drawMarker (new LatLng (Double.parseDouble (mLocationList.get (i).getLatitude()), Double.parseDouble (mLocationList.get (i).getLongitude())), i); 

在你onMarkerClick(),然后你只需要仰望Marker在你的地图,让您的索引mLocationList

int index = mMarkerMap.get(marker); 
+0

Thanks @HexAndBugs。你的解决方案也适用于我。 – Chandru

相关问题