Skip to content
Advertisement

InfoWindow & GeoCoder

I have an issue with getting longitude and latitude values geocoded and then display the relevant address in an info window. I have tried multiple ways without success, however I must admit that I am not so familiar with javascript. I am continuously getting back as value : “undefined”.

Here is a snippet of my code showing the main components:

var position = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var geocoder = new google.maps.Geocoder(); var address;

JavaScript

Can anyone tell me how I can translate the lat/lng in position to an address in order to show them in my infowindow?

EDIT

Updated Code:
var position = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); var geocoder = new google.maps.Geocoder(); var infowindow = new google.maps.InfoWindow(); var address;

JavaScript

if(!marker){ marker = new google.maps.Marker({ position: position, map: this.map, icon: markericon, draggable:false }); }else{ marker.setPosition(point);
}

Advertisement

Answer

The geocoder is asynchronous. You need to use the data it returns in the callback function. Something like this (not tested):

JavaScript

Working example

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement