1

I'm using Google Maps Geocoding function. I got some addresses which I load into a PHP variable. The addresses are defined by a textbox which the user can type in a zip code. I give an example:

A user types in the textbox a postal code (12345 for example). Now it loads addresses from the system (this is internal - but it works to load the addresses). And then it display some addresses. These addresses are now stored in a PHP variable ($address). And now I want to get the latitude and longitude from these different addresses.

I had a look at this thread: Javascript geocoding from address to latitude and longitude numbers not working

I used this code from the article:

var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
alert(latitude + ' - ' + longitude);

This is nice but it just give me the latlng from the address I typed in the textbox, not from all the addresses I got.

Is there a way to get the latitude and longitude from all the different addresses? If yes, could someone please explain me? I'm new to JavaScript and Google Maps API.

Thanks in advance

Community
  • 1
  • 1
Tyler
  • 882
  • 4
  • 18
  • 32

1 Answers1

0

You can use the example given here. See the code below for specific functionality:

    geocoder = new google.maps.Geocoder();
function findAddress(address) 
{
    // You can pass the address from your PHP to Javascript using AJAX. Here, I'm assuming that you have done it and that you're passing it as a string argument to this function
    geocoder.geocode( { 'address': address}, function(results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK) 
        {
             return position: results[0].geometry.location;
        } 
        else 
        {
            // Show some message that the address cannot be found on map.
        }
    });
}
Basit
  • 1,830
  • 2
  • 31
  • 50
  • I got this but is this really to get `latitude` and `longitude` from the addresses which are loaded? I thought this is just to create a marker with a specific position? – Tyler Dec 10 '13 at 07:38
  • Yes, you're right! I've edited the code. Now it will return you the location (latlng) of the address you've provided. – Basit Dec 10 '13 at 07:44
  • The address is now stored in `'address': address`? So I can load the PHP variable `$address` to JavaScript `'address': address`? Am I right or have I missunderstood it? – Tyler Dec 10 '13 at 07:48
  • No, the function returns the latlng Use it like this `//Your code that fetches the address from PHP via AJAX.` `var latlng = findAddress(address);` Now you can do whatever you want to with that string. Please mark it as an answer if it solves your question. – Basit Dec 10 '13 at 19:04
  • Mh, thank you for your support but I still don't get the latlng from the addresses :/ – Tyler Dec 11 '13 at 09:30
  • What exactly are you getting when you execute that function? Make sure you've included the Google Maps JS file into your page. – Basit Dec 11 '13 at 09:34
  • I've included the JS file. I don't get anything. I want to alert the latlng from every single address but I don't get an alert. If I use my code above I get the LatLng from the address I typed in... – Tyler Dec 11 '13 at 09:40