Wordpressor
Wordpressor

Reputation: 7553

Google Maps - converting address to latitude & longitude - PHP backend?

is it possible to convert (in PHP back-end):

<?php
  $Address = "Street 1, City, Country"; // just normal address
?>

to

<?php
  $LatLng = "10.0,20.0"; // latitude & lonitude
?>

The code I'm currently using is the default one:

<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?v=3.exp&#038;sensor=false'></script>

<script>

function initialize() {
  var myLatlng = new google.maps.LatLng(10.0,20.0);
  var mapOptions = {
    zoom: 16,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById('map'), mapOptions);

  var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
      title: 'This is a caption'
  });
}

google.maps.event.addDomListener(window, 'load', initialize); 

</script>

<div id="map"></div>

I've been reading https://developers.google.com/maps/documentation/geocoding for a while now but I'm not experienced enough I guess.

Thanks.

Upvotes: 12

Views: 46686

Answers (4)

Moh
Moh

Reputation: 259

<?php

/* 
* Given an address, return the longitude and latitude using The Google Geocoding API V3
*
*/

function Get_LatLng_From_Google_Maps($address) {
    $address = urlencode($address);

    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false";

    // Make the HTTP request
    $data = @file_get_contents($url);
    // Parse the json response
    $jsondata = json_decode($data,true);

    // If the json data is invalid, return empty array
    if (!check_status($jsondata))   return array();

    $LatLng = array(
        'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"],
        'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"],
    );

    return $LatLng;
}

/* 
* Check if the json data from Google Geo is valid 
*/

function check_status($jsondata) {
    if ($jsondata["status"] == "OK") return true;
    return false;
}

/*
*  Print an array
*/

function d($a) {
    echo "<pre>";
    print_r($a);
    echo "</pre>";
}

For sample code of how using the function above, feel free to visit my blog

Upvotes: 7

 this will work please try this one i have tested it .
 <?php
            $address = 'Street 1, City, Country'; // Your address
            $prepAddr = str_replace(' ','+',$address);

            $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');

            $output= json_decode($geocode);

             $lat = $output->results[0]->geometry->location->lat;
             $long = $output->results[0]->geometry->location->lng;

             echo $address.'<br>Lat: '.$lat.'<br>Long: '.$long;

    ?>

Upvotes: 2

Neil Robertson
Neil Robertson

Reputation: 3345

This works for me:

<?php
  $Address = urlencode($Address);
  $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true";
  $xml = simplexml_load_file($request_url) or die("url not loading");
  $status = $xml->status;
  if ($status=="OK") {
      $Lat = $xml->result->geometry->location->lat;
      $Lon = $xml->result->geometry->location->lng;
      $LatLng = "$Lat,$Lon";
  }
?>

References:

Upvotes: 35

datasage
datasage

Reputation: 19573

Yes you can. You would do a request with file_get_contents or curl to a url like this:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

What is returned is a json encoded response. You can parse this into arrays using json_decode.

The documentation page has an example of the what the response may look like, use that to find the data that you need.

Upvotes: 5

Related Questions