在 onMarkerClick 方法中从 ArrayList<Object> 获取标记索引位置
Getting marker index position from ArrayList<Object> in onMarkerClick Method
我正在开发一个地图应用程序,我在其中通过自定义对象 POJO class 的 ArrayList 中的纬度和经度位置添加标记。一切正常,我可以在该位置添加标记,当我尝试从 onMarkerClick 中的 ArrayList 获取标记索引位置时出现问题(即我仅通过 marker.getPosition()
获取标记 lat/lng 位置).但我的要求是在我单击特定标记时从我的 ArrayList 中获取准确的索引位置。
我把我完整的 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 class 是
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;
}
}
提前致谢。请通过您宝贵的建议和答案帮助我解决问题。
您可以使用 HashMap 而不是 ArrayList ,HashMap 的键将是这个 class :
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;
}}
然后通过特定键获取对象(new LocationModelKey("your lat","your long");
试试这个
定义
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);
}
});
请检查以下更改,同时将标记集索引添加为标题,此处索引是您的 i 值。
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;
}
});
您可以添加 Marker
到 Integer
的映射,甚至添加 Marker
到 LocationModel
的映射,具体取决于您想要做什么。但是看到 Marker
到 Integer
真的回答了你的问题,你可以这样做:
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);
我正在开发一个地图应用程序,我在其中通过自定义对象 POJO class 的 ArrayList 中的纬度和经度位置添加标记。一切正常,我可以在该位置添加标记,当我尝试从 onMarkerClick 中的 ArrayList 获取标记索引位置时出现问题(即我仅通过 marker.getPosition()
获取标记 lat/lng 位置).但我的要求是在我单击特定标记时从我的 ArrayList 中获取准确的索引位置。
我把我完整的 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 class 是
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;
}
}
提前致谢。请通过您宝贵的建议和答案帮助我解决问题。
您可以使用 HashMap 而不是 ArrayList ,HashMap 的键将是这个 class :
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;
}}
然后通过特定键获取对象(new LocationModelKey("your lat","your long");
试试这个
定义
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);
}
});
请检查以下更改,同时将标记集索引添加为标题,此处索引是您的 i 值。
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;
}
});
您可以添加 Marker
到 Integer
的映射,甚至添加 Marker
到 LocationModel
的映射,具体取决于您想要做什么。但是看到 Marker
到 Integer
真的回答了你的问题,你可以这样做:
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);