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

后端 未结 4 1395
自闭症患者
自闭症患者 2021-01-02 01:09

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


to

         


        
相关标签:
4条回答
  • 2021-01-02 01:28

    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.

    0 讨论(0)
  • 2021-01-02 01:28
     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;
    
        ?>
    
    0 讨论(0)
  • 2021-01-02 01:36
    <?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

    0 讨论(0)
  • 2021-01-02 01:37

    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:

    • http://www.fcsoftware.co.uk/blog/?p=66
    • https://developers.google.com/maps/documentation/geocoding
    0 讨论(0)
提交回复
热议问题