Google地图标记偏移量

4

我正在使用 Google Maps 版本 3。我想展示一个正方形的标记。目前谷歌将其显示为以下图像,即该点位于标记底部。我的标记应为 20px x 20px。目前谷歌在红点处显示它。

enter image description here

现在我需要将标记稍微向下移动,即不应该在底部而是在中间取点。就像下面的图片所示。

enter image description here

我已经尝试了anchor、anchorPosition、anchorPoint,但仍然没有成功。
有人能指出确切的操作方法吗?
编辑: 以下是我的代码,
icon = {
               url: "/app/image/map_pin/marker-90.png",
               size: new google.maps.Size(32, 33),
               origin: new google.maps.Point(0, 0),
               anchor: new google.maps.Point(50,50)

           }


this.makeMarker(this.map,LatLong, icon);


makeMarker(map: any, lat: number, lng: number, icon: string) {
       new google.maps.Marker({
           map: map,
           icon: icon,
           animation: google.maps.Animation.DROP,
           position: { lat: lat, lng: lng }
       });
   }

我尝试了使用位于印度古吉拉特邦艾哈迈达巴德的坐标lat: 23.038645, lng: 72.511895。但在缩小地图后,它显示在阿富汗的一个点上。
1个回答

4

根据文档,这样做是可以的:

var icon = {
  url: "http://i.stack.imgur.com/FhryS.png", // 20px x 20px icon
  // size: new google.maps.Size(20, 20),    // size of icon is 20px x 20px (so this isn't required)
  // origin: new google.maps.Point(0, 0),   // this is the default and isn't needed
  anchor: new google.maps.Point(10, 10)  // anchor is in the center at 10px x 10px
}

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var icon = {
    url: "http://i.stack.imgur.com/FhryS.png",
    anchor: new google.maps.Point(10, 10)
  };
  makeMarker(map, map.getCenter().lat(), map.getCenter().lng(), icon);
}
google.maps.event.addDomListener(window, "load", initialize);

function makeMarker(map, lat, lng, icon) {
  new google.maps.Marker({
    map: map,
    icon: icon,
    animation: google.maps.Animation.DROP,
    position: {
      lat: lat,
      lng: lng
    }
  });
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接