gpt4 book ai didi

google-maps - Google map API getProjection() 在 V3 中不起作用

转载 作者:行者123 更新时间:2023-12-02 08:08:14 26 4
gpt4 key购买 nike

根据 API 引用, map 对象应该有一个 getProjection方法:
http://code.google.com/apis/maps/documentation/v3/reference.html#Map

在本例中加载 map 时应该提醒 x,y 点,但是将值抛出为未定义。这是下面在 onload 中调用的示例代码。

function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
alert("projection:" + map.getProjection());
}

最佳答案

在 map 完成初始化之前它不可用。您必须等待“projection_changed”事件才能访问它。

function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListenerOnce(map,"projection_changed", function() {
alert("projection:"+map.getProjection());
});
}

proof of concept fiddle

代码片段:

function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListenerOnce(map, "projection_changed", function() {
console.log("projection:" + map.getProjection());
document.getElementById('output').innerHTML = "map.getProjection()=" + JSON.stringify(map.getProjection(), null, ' ');
});
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */

#map-canvas {
height: 80%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>

<head>
<title>Simple Map</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<!-- jsFiddle will insert css and js -->
</head>

<body>
<div id="output"></div>
<div id="map-canvas"></div>

<!-- Async script executes immediately and must be after any DOM elements used in callback. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize&libraries=&v=weekly" async></script>
</body>

</html>

关于google-maps - Google map API getProjection() 在 V3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17191664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com