admin管理员组文章数量:1323716
I am trying to convert a zip code into lat and lng using geocoder. I looked at the Google Maps Api example and followed several tutorials, but for some reason for lat and lng it returns "". Here is my code.
(function(window, google, address) {
var geocoder = new google.maps.Geocoder();
var lat = "";
var lng = "";
geocoder.geocode({
'address': address
}, function(results, status) {
var test = results[0];
lat = results[0].geometry.location.lat(); // lat is still equal to ""
lng = results[0].geometry.location.lng(); // lng is still equal to ""
});
var options = {
center: {
lat: lat,
lng: lng
},
zoom: 5
},
element = document.getElementById('map-canvas')
var map = new google.maps.Map(element, options)
}(window, window.google, 98107));
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src=""></script>
<div id="map-canvas"></div>
I am trying to convert a zip code into lat and lng using geocoder. I looked at the Google Maps Api example and followed several tutorials, but for some reason for lat and lng it returns "". Here is my code.
(function(window, google, address) {
var geocoder = new google.maps.Geocoder();
var lat = "";
var lng = "";
geocoder.geocode({
'address': address
}, function(results, status) {
var test = results[0];
lat = results[0].geometry.location.lat(); // lat is still equal to ""
lng = results[0].geometry.location.lng(); // lng is still equal to ""
});
var options = {
center: {
lat: lat,
lng: lng
},
zoom: 5
},
element = document.getElementById('map-canvas')
var map = new google.maps.Map(element, options)
}(window, window.google, 98107));
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis./maps/api/js"></script>
<div id="map-canvas"></div>
Share
Improve this question
edited Jul 20, 2017 at 22:12
geocodezip
161k14 gold badges226 silver badges255 bronze badges
asked Jul 20, 2017 at 21:02
AaronAaron
4,48021 gold badges92 silver badges151 bronze badges
2 Answers
Reset to default 4The geocoder is asynchronous. You need to use the data returned in the callback function when/where it exists.
(function(window, google, address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
var options = {
center: results[0].geometry.location,
zoom: 5
},
element = document.getElementById('map-canvas')
var map = new google.maps.Map(element, options)
});
}(window, window.google, "98107"));
Another issue with the posted code is the address
passed into the geocoder must be a string (so "98107", not 98107), if it isn't the API generates the error: InvalidValueError: in property address: not a string
(function(window, google, address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address
}, function(results, status) {
var options = {
center: results[0].geometry.location,
zoom: 5
},
element = document.getElementById('map-canvas')
var map = new google.maps.Map(element, options)
});
}(window, window.google, "98107"));
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis./maps/api/js"></script>
<div id="map-canvas"></div>
geocode() is an asynchronous function, and you need to wait for its callback to get the actual results.
In your code, you create the center object outside of the callback before the latitude and longitude values are even returned.
Try the following :
geocoder.geocode({
'address': address
}, function(results, status) {
var test = results[0];
var options = {
center: {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
},
zoom: 5
}
});
本文标签: javascriptGoogle maps api geocoder is not workingStack Overflow
版权声明:本文标题:javascript - Google maps api geocoder is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742124868a2421892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论