我正在尝试使用以下代码从像素坐标开始在Google地图上创建标记。我试图使用fromDivPixelToLatLng()将像素xy坐标转换为Lat Long,但我无法得到任何标记。我需要在没有点击事件的情况下在地图上设置标记。谁能给我一些建议?
<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>
答案 0 :(得分:0)
等待地图初始化(空闲事件):
<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>