Google会在所有执行中检索相同的值

时间:2013-06-04 04:16:17

标签: android google-places-api android-maps-v2

我正在使用面向谷歌地图v2的Android项目。在我的程序中,我在触摸标记的信息框时从地点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);

2 个答案:

答案 0 :(得分:1)

你做错了是在循环中设置OnInfoWindowClickListener

将该代码移到外面,并使用slatslon检索Marker arg0getPosition

答案 1 :(得分:0)

还有其他人也在努力解决这个问题。

您假设slat和slong值将传递给onInfoWindowClick()方法,但您总是在那里得到相同的值。

Here is a discussion其他一些人正在谈论这个话题。