0

我正在尝试在 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>

花了一段时间,咨询了谷歌地图 api 参考,但就是不知道为什么它不起作用。

任何见解表示赞赏 - 谢谢。

4

1 回答 1

2
mMap.setOnMarkerClickListener(new OnMarkerClickListener() 
      {
          @Override
          public boolean onMarkerClick(Marker marker)
          {
              int yMatrix = 200, xMatrix =40;

              DisplayMetrics metrics1 = new DisplayMetrics();
              getWindowManager().getDefaultDisplay().getMetrics(metrics1);
              switch(metrics1.densityDpi)
              {
              case DisplayMetrics.DENSITY_LOW:
                  yMatrix = 80;
                  xMatrix = 20;
                  break;
              case 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 point = 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;
          }
      });
于 2013-10-14T11:37:12.767 回答