我正在尝试在Google地图上显示几个标记,并允许用户在单击某个标记时将地图居中。
但是,当用户单击任何标记时,地图始终以我在代码中创建的最后一个标记为中心。
下面是我使用的完整代码:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?key=MY_API_KEY&sensor=false">
</script>
<script type="text/javascript">
function initialize() {
var myOptions = {
center: new google.maps.LatLng(-34, 150),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var myLatLng = new google.maps.LatLng(34,-150);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title:'test' + 1
});
google.maps.event.addListener(marker, 'click', function(){map.setCenter(marker.getPosition());});
myLatLng = new google.maps.LatLng(20,-87);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title:'test' + 2
});
google.maps.event.addListener(marker, 'click', function(){map.setCenter(marker.getPosition());});
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
我在这上面花了一段时间,查阅了Google地图api参考资料,但就是想不出为什么它不能工作。
任何见解都很感谢-谢谢。
发布于 2013-10-14 11:37:13
mMap.setOnMarkerClickListener( OnMarkerClickListener() { @Override public boolean onMarkerClick(标记标记){ int yMatrix = 200,xMatrix =40;DisplayMetrics metrics1 = new DisplayMetrics();intswitch(metrics1.densityDpi) {案例DisplayMetrics.DENSITY_LOW: yMatrix = 80;xMatrix = 20;break;案例DisplayMetrics.DENSITY_MEDIUM: yMatrix = 100;xMatrix = 25;break;case DisplayMetrics.DENSITY_HIGH: yMatrix = 150;xMatrix = 30;break;case DisplayMetrics.DENSITY_XHIGH: yMatrix = 200;xMatrix = 40;break;case DisplayMetrics.DENSITY_XXHIGH: yMatrix = 200;xMatrix = 50;break;} Projection projection = mMap.getProjection();LatLng latLng = marker.getPosition();point LatLng = projection.toScreenLocation(latLng);Point point2 = new Point(point.x+xMatrix,point.y-yMatrix);LatLng point3 = projection.fromScreenLocation(point2);CameraUpdate zoom1 = CameraUpdateFactory.newLatLng(point3);mMap.animateCamera(zoom1);marker.showInfoWindow();return true;} });
https://stackoverflow.com/questions/10466294
复制相似问题