根据城市名称 google 地图获取坐标
Getting coordinates based on city name google map
我正在尝试使用 google 地图创建基于城市的坐标,这是我现在的示例,我总是出错?
var address = 'Zurich, Ch';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var Lat = results[0].geometry.location.lat();
var Lng = results[0].geometry.location.lng();
} else {
alert("Something got wrong " + status);
}
});
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(Lat, Lng),
};
地理编码器是异步的。您需要在回调函数中使用返回的数据when/where可用。
相关问题:Using Address Instead Of Longitude And Latitude With Google Maps API
代码片段:
function initialize() {
var address = 'Zurich, Ch';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var Lat = results[0].geometry.location.lat();
var Lng = results[0].geometry.location.lng();
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(Lat, Lng)
};
var map = new google.maps.Map(
document.getElementById("map_canvas"), myOptions);
} else {
alert("Something got wrong " + status);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
我正在尝试使用 google 地图创建基于城市的坐标,这是我现在的示例,我总是出错?
var address = 'Zurich, Ch';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var Lat = results[0].geometry.location.lat();
var Lng = results[0].geometry.location.lng();
} else {
alert("Something got wrong " + status);
}
});
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(Lat, Lng),
};
地理编码器是异步的。您需要在回调函数中使用返回的数据when/where可用。
相关问题:Using Address Instead Of Longitude And Latitude With Google Maps API
代码片段:
function initialize() {
var address = 'Zurich, Ch';
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var Lat = results[0].geometry.location.lat();
var Lng = results[0].geometry.location.lng();
var myOptions = {
zoom: 11,
center: new google.maps.LatLng(Lat, Lng)
};
var map = new google.maps.Map(
document.getElementById("map_canvas"), myOptions);
} else {
alert("Something got wrong " + status);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>