我正在处理地图应用程序,我通过自定义对象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;
}
}
感谢。请通过宝贵的建议和答案帮助我解决问题。
感谢@Soham。如何基于点迭代地图。如果我们在地图上反转键和值,该怎么办? – Chandru
private Map marker = new HashMap(); –
Chandru
是的,你可以扭转地图中的值,这将是罚款@Chandru – Soham