admin管理员组文章数量:1401431
Im trying to use google maps api to convert my address to longitude and latitude and THEN display the map on the page.
Just a note im using PHP/Laravel to retrieve the address from the DB. So although its a hard coded value of new york that will be dynamic after i get this working.
Html
<div id="map" style="height:250px"></div>
Javascript
<script>
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);
}
});
function initMap() {
var myLatLng = {lat: latitude, lng: longitude};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
</script>
<script async defer
src=";callback=initMap">
</script>
At the moment i'm getting the error of "Uncaught ReferenceError: google is not defined"
Im trying to use google maps api to convert my address to longitude and latitude and THEN display the map on the page.
Just a note im using PHP/Laravel to retrieve the address from the DB. So although its a hard coded value of new york that will be dynamic after i get this working.
Html
<div id="map" style="height:250px"></div>
Javascript
<script>
var geocoder = new google.maps.Geocoder();
var address = "new york";
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude);
}
});
function initMap() {
var myLatLng = {lat: latitude, lng: longitude};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
</script>
<script async defer
src="https://maps.googleapis./maps/api/js?key=keyremoved&callback=initMap">
</script>
At the moment i'm getting the error of "Uncaught ReferenceError: google is not defined"
Share Improve this question edited Aug 12, 2016 at 9:47 Gandalf the White 2,4652 gold badges20 silver badges39 bronze badges asked Aug 12, 2016 at 9:09 KayTokyoKayTokyo 1372 gold badges3 silver badges12 bronze badges 3-
you're executing the top half of your code before
initMap
is called - initMap is called once the google maps api is loaded - put the code that is aboveinitMap
insideinitMap
and you'll be closer to achieving your goal – Jaromanda X Commented Aug 12, 2016 at 9:11 -
Move you geocoder code inside the
initMap
function. You'll see in the URL of the maps js URI there is a a callback function. This is executed once the maps script is loaded. You're calling newgoogle.maps.Geocoder();
before the script has loaded – Brett Gregson Commented Aug 12, 2016 at 9:12 - duplicate of Using Address Instead Of Longitude And Latitude With Google Maps API – geocodezip Commented Aug 12, 2016 at 9:46
2 Answers
Reset to default 2Change the order of scripts
<script async defer
src="https://maps.googleapis./maps/api/js?key=keyremoved&callback=initMap">
</script>
<script>
var geocoder = new google.maps.Geocoder();
var address = "new york";
// rest of the code
</script>
As mentioned above the code needed to be put inside of my callback function.
<script>
function initMap(address) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
}
console.log(latitude);
console.log(longitude);
var myLatLng = {lat: latitude, lng: longitude};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
});
}
</script>
<script async defer
src="https://maps.googleapis./maps/api/js?key=AIzaSyCY2buDtbYIot8Llm_FkQXHW36f0Cme6TI&callback=initMap">
</script>
版权声明:本文标题:javascript - How to convert an address to longitude and latitude then display a map on the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744307390a2599862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论