javascript 不能使用 google.maps.LatLng 对象的纬度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16748716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot use google.maps.LatLng object's lat
提问by Minh Triet
I have this piece of code
我有这段代码
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var map;
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);
var polygonCoords = [
new google.maps.LatLng(25.774252, -80.190262),
new google.maps.LatLng(18.466465, -66.118292),
new google.maps.LatLng(32.321384, -64.757370),
new google.maps.LatLng(25.774252, -80.190262)
];
var north = new google.maps.LatLng(), west = new google.maps.LatLng(), east = new google.maps.LatLng(), south = new google.maps.LatLng();
north = polygonCoords[0];
west = polygonCoords[0];
east = polygonCoords[0];
south = polygonCoords[0];
for (i = 1; i < polygonCoords.length; i++) {
if (north.lat < polygonCoords[i].lat) {
north = polygonCoords[i];
}
if (south.lat > polygonCoords[i].lat) {
south = polygonCoords[i];
}
if (west.lng > polygonCoords[i].lng) {
west = polygonCoords[i];
}
if (east.lng < polygonCoords[i].lng) {
east = polygonCoords[i];
}
}
var center = new google.maps.LatLng(0,0);
center.lat = (north.lat + south.lat) / 2;
center.lng = (west.lng + east.lng) / 2;
var num = (north.lat + south.lat) / 2;
console.log(num);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
Why does num's result is NaN. How do I use lat, lng at number? Possible duplication here, I am trying to use the first solution since the second solution does not work for me. Now I get stuck in this problem. Thank you in advance.
为什么 num 的结果是 NaN。我如何使用 lat, lng at number?此处可能重复,我尝试使用第一个解决方案,因为第二个解决方案对我不起作用。现在我陷入了这个问题。先感谢您。
回答by geocodezip
A google.maps.LatLng objectdoesn't have a documented lat property (or a lng property). It has a .lat() method which returns its latitude and a .lng() method which returns its longitude.
甲google.maps.LatLng对象不具有记录LAT属性(或LNG属性)。它有一个返回其纬度的 .lat() 方法和一个返回其经度的 .lng() 方法。
var num = (north.lat() + south.lat()) / 2;
var num = (north.lat() + south.lat()) / 2;
code snippet:
代码片段:
var map;
function initialize() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var polygonCoords = [
new google.maps.LatLng(25.774252, -80.190262),
new google.maps.LatLng(18.466465, -66.118292),
new google.maps.LatLng(32.321384, -64.757370),
new google.maps.LatLng(25.774252, -80.190262)
];
var north = new google.maps.LatLng(),
west = new google.maps.LatLng(),
east = new google.maps.LatLng(),
south = new google.maps.LatLng();
north = polygonCoords[0];
west = polygonCoords[0];
east = polygonCoords[0];
south = polygonCoords[0];
for (i = 1; i < polygonCoords.length; i++) {
if (north.lat() < polygonCoords[i].lat()) {
north = polygonCoords[i];
}
if (south.lat() > polygonCoords[i].lat()) {
south = polygonCoords[i];
}
if (west.lng() > polygonCoords[i].lng()) {
west = polygonCoords[i];
}
if (east.lng() < polygonCoords[i].lng()) {
east = polygonCoords[i];
}
}
var center = {};
center.lat = (north.lat() + south.lat()) / 2;
center.lng = (west.lng() + east.lng()) / 2;
var num = (north.lat() + south.lat()) / 2;
console.log(num);
var polygon = new google.maps.Polygon({
path: polygonCoords,
map: map
})
map.setCenter(center);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map-canvas"></div>