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

谷歌地图-将地址转换为纬度&;经度-PHP后端?,php,google-maps,geocoding,Php,Google Maps,Geocoding,是否可以转换(在PHP后端中): 到 我当前使用的代码是默认代码: <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

是否可以转换(在PHP后端中):



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

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

函数初始化(){
var mylatng=新的google.maps.LatLng(10.0,20.0);
变量映射选项={
缩放:16,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(document.getElementById('map'),mapOptions);
var marker=new google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“这是一个标题”
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
我已经读了一段时间了,但我想我还没有足够的经验


谢谢。

是的,你可以。您可以使用
file\u get\u contents
curl
请求如下url:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+圆形剧场+公园道,+山景,+CA和传感器=假

返回的是json编码的响应。您可以使用
json\u decode
将其解析为数组

文档页面有一个示例,说明了响应可能是什么样的,请使用该示例查找所需的数据。

这对我很有用:

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

    ?>
状态;
如果($status==“OK”){
$Lat=$xml->result->geometry->location->Lat;
$Lon=$xml->result->geometry->location->lng;
$LatLng=“$Lat,$Lon”;
}
?>
参考资料:

这将起作用。请试试这一款,我已经测试过了。

请注意,我更新了代码,使其更加面向对象/准确。这适用于单个地址,但我有多个地址,那么我该怎么做呢。
<?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>";
}
<?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";
  }
?>
 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;

    ?>