我正在使用一个面向 Google map v2 的 android 项目。在我的程序中,我在触摸标记的信息框时从 places api 检索名称、附近、纬度和经度。但它每次检索相同的值。但它在信息框中显示正确的结果。如何解决此错误.有人请解决这个问题。
代码
for(final Place place : nearPlaces.results){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting latitude of the place
double latitude = place.geometry.location.lat;
double longitude = place.geometry.location.lng;
// Getting name
String NAME = place.name;
// Getting vicinity
String VICINITY = place.vicinity;
//final String REFERENCE = place.reference;
final String slat = String.valueOf(latitude);
final String slon = String.valueOf(longitude);
LatLng latLng = new LatLng(latitude, longitude);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(NAME + " : " + VICINITY);
markerOptions.icon(bitmapDescriptor);
// Placing a marker on the touched position
mGoogleMap.addMarker(markerOptions);
mGoogleMap.setOnInfoWindowClickListener(
new OnInfoWindowClickListener(){
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
arg0.hideInfoWindow();
alert.showpickAlertDialog2(PlacesMapActivity.this, slat, slon, place.reference,KEY_TAG);
}
}
);
}
LatLng mylatLng = new LatLng(mylatitude, mylongitude);
// Creating CameraUpdate object for position
CameraUpdate updatePosition = CameraUpdateFactory.newLatLng(mylatLng);
// Creating CameraUpdate object for zoom
CameraUpdate updateZoom = CameraUpdateFactory.zoomBy(10);
// Updating the camera position to the user input latitude and longitude
mGoogleMap.moveCamera(updatePosition);
// Applying zoom to the marker position
mGoogleMap.animateCamera(updateZoom);
最佳答案
你做错的是在循环中设置 OnInfoWindowClickListener
。
将该代码取出并使用 getPosition
从 Marker arg0
中检索 slat
和 slon
。
关于android - Google places 在所有执行中检索相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909722/