从像素xy坐标创建谷歌地图标记

从像素xy坐标创建谷歌地图标记

本文介绍了从像素xy坐标创建谷歌地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用以下代码在像素坐标开始的Google地图上创建标记。我试图使用fromDivPixelToLatLng()将像素xy坐标转换为Lat Long,但我无法获得任何标记。我需要在地图上设置标记而无需点击事件。任何人都可以给我一些建议吗?

I'm trying to create markers on a google map starting from pixel coordinates, using the code below. Im trying to use fromDivPixelToLatLng() to convert the pixel xy coordinates into Lat Long, but I can't get any markers to come up. I need the markers to set on the map without a click event. Can anyone give me some advice?

<script type="text/javascript">

var map;
function initialize() {

  var mapOptions = {
      center: new google.maps.LatLng(10,0),
      zoom: 2,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      };
  map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);

  var overlay = new google.maps.OverlayView();
  overlay.draw = function() {};
  overlay.setMap(map);

  var pixelLatLng = overlay.getProjection().fromDivPixelToLatLng(new google.maps.Point(200,200));

  var marker = new google.maps.Marker({
      position: pixelLatLng,
      map: map,
      });


};//end initialize

google.maps.event.addDomListener(window, 'load', initialize);
</script>


推荐答案

等待地图初始化):

Wait for the map to be initialized (the idle event):

<script type="text/javascript">
var map;
function initialize() {
  var mapOptions = {
      center: new google.maps.LatLng(10,0),
      zoom: 2,
      mapTypeId: google.maps.MapTypeId.ROADMAP
      };
  map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);

  var overlay = new google.maps.OverlayView();
  overlay.draw = function() {};
  overlay.setMap(map);

  google.maps.event.addListenerOnce(map,'idle', function() {
    var pixelLatLng = overlay.getProjection().fromDivPixelToLatLng(new google.maps.Point(200,200));

    var marker = new google.maps.Marker({
          position: pixelLatLng,
          map: map,
        });
  });
};//end initialize

google.maps.event.addDomListener(window, 'load', initialize);
</script>

这篇关于从像素xy坐标创建谷歌地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:08