自定义图像图标无法定位

时间:2018-04-24 03:49:31

标签: google-maps google-maps-api-3 google-maps-markers

我知道这听起来很复杂但是当我在这里搜索时它对我没有帮助。我的问题是我有自定义图标或图像尺寸为256 x 256,scaledSize这个为50,现在的问题是它没有正确定位到latlng。还有一个问题是,当我缩放地图时,标记会移动或者会移动到它的位置......不像这个例子它工作得很好 https://developers.google.com/maps/documentation/javascript/examples/icon-complex

这是我的代码

 var imageicon= {
                url:  '/image/' + filename, // url
                scaledSize: new google.maps.Size(50, 50), // scaled size
                origin: new google.maps.Point(0, 0), // origin
                anchor: new google.maps.Point(0, 50) // anchor
            };


 custommarker= new google.maps.Marker({
                flat: true,
                icon: imageicon,
                map: map,
                optimized: false,
                position: coordinate,
                visible: true
            });

这是我的标记

enter image description here

1 个答案:

答案 0 :(得分:1)

您的图标为50像素x 50像素。您当前将锚点设置为(0,50)(图标的左下角)。您希望将锚点设置为" point"在底部中心(25,50)

var imageicon = {
  url: 'https://i.stack.imgur.com/kEvED.png', // url
  scaledSize: new google.maps.Size(50, 50), // scaled size
  origin: new google.maps.Point(0, 0), // origin
  anchor: new google.maps.Point(25, 50) // anchor
};

proof of concept fiddle

screenshot of resulting map

代码段



function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: { // New York, NY
      lat: 40.7127753,
      lng: -74.0059728
    },
    zoom: 3
  });
  var imageicon = {
    url: 'https://i.stack.imgur.com/kEvED.png', // url
    scaledSize: new google.maps.Size(50, 50), // scaled size
    origin: new google.maps.Point(0, 0), // origin
    anchor: new google.maps.Point(25, 50) // anchor
  };

  var custommarker = new google.maps.Marker({
    flat: true,
    icon: imageicon,
    map: map,
    optimized: false,
    position: map.getCenter(),
    visible: true
  });
}

html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
}

<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>
&#13;
&#13;
&#13;