谷歌地图 - 将地址转换为经纬度 - PHP后端?

12

是否有可能在PHP后端进行转换:

<?php
  $Address = "Street 1, City, Country"; // just normal address
?>
<?php
  $LatLng = "10.0,20.0"; // latitude & lonitude
?>

我目前使用的代码是默认的:

<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>

我已经阅读了一段时间的https://developers.google.com/maps/documentation/geocoding,但我想我还不够有经验。

谢谢。

4个回答

35

对我来说这个方法有效:

<?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";
  }
?>

参考文献:


1
请注意,我已更新代码以更加面向对象/准确。http://www.fcsoftware.co.uk/blog/?p=71 - Dave

7
<?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>";
}

如果您需要使用上述功能的示例代码,请随时访问我的博客


5
是的,您可以。您可以使用file_get_contentscurl向以下网址发出请求:http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false。 返回的是一个json编码的响应。您可以使用json_decode将其解析为数组。文档页面上有一个响应可能看起来像什么的示例,请使用它来查找您需要的数据。

2
 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;

    ?>

这适用于单个地址,但如果我有多个地址,我该怎么做? - Viral M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接