【问题标题】:Google Maps question to obtain coordinates in php谷歌地图问题在php中获取坐标
【发布时间】:2011-01-25 18:34:35
【问题描述】:

这是在 php 我在数组上有以下变量

Array ( [0] => { "name": "BRAVO Mario1050 [1] => Capital Federal [2] => Argentina" [3] => "Status": { "code": 200 [4] => "request": "geocode" } [5] => "Placemark": [ { "id": "p1" [6] => "address": "Buenos Aires [7] => Capital Federal [8] => Argentina" [9] => "AddressDetails": { "Accuracy" : 4 [10] => "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "Capital Federal" [11] => "Locality" : { "LocalityName" : "Ciudad Autónoma de Buenos Aires" } } [12] => "CountryName" : "Argentina" [13] => "CountryNameCode" : "AR" } } [14] => "ExtendedData": { "LatLonBox": { "north": -34.5349161 [15] => "south": -34.6818539 [16] => "east": -58.2451019 [17] => "west": -58.5012207 } } [18] => "Point": { "coordinates": [ -58.3731613 [19] => -34.6084175 [20] => 0 ] } } ] } ) 

我正在使用数组、explodes 和 str_replace 将 -58.3731613、-34.6084175 转换为两个变量,有没有简单的方法可以做到这一点?

我有一个额外的问题,我所做的是工作,但显然谷歌改变了一些东西,因为现在我得到了与 1 个月前不同的结果,问题是......有人知道为什么谷歌改变了一些东西吗?

感谢一切

以防旧代码曾经可以工作:

        $longitude = "";
        $latitude = "";
        $precision = "";
        //Three parts to the querystring: q is address, output is the format (
        $key = "googlekey";
        $address = urlencode(str_replace(',',' ',$calle).$altura.", ".$localidadList.", Argentina");
        $url = "http://maps.google.com/maps/geo?q=".$address."&output=csv&key=".$key;
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        $latitude= str_replace('Point','',$data[20]);
        $latitude= str_replace('coordinates','',$latitude);
        $latitude= str_replace('"','',$latitude);
        $latitude= str_replace(':','',$latitude);
        $latitude= str_replace('{','',$latitude);
        $latitude= trim(str_replace('[','',$latitude));
        $longitude= trim(str_replace('}','',str_replace('"west": ','',$data[21])));

【问题讨论】:

  • 在我看来,您正在尝试手动解析 KML 格式的 Google Maps API 2.0(已弃用)地理编码请求的 var_dump,然后将其解析为 PHP 中的数组。您能否显示完整的来源?可能有一个更简单的解决方案。
  • 感谢乔纳森的回答,我添加了一些神秘的代码 =D

标签: php google-maps str-replace text-extraction


【解决方案1】:

3 行代码怎么样? ;-)

$b = file_get_contents("http://maps.google.com/maps/geo?q=". urlencode("1050+BRAVO Mario,+Capital Federal,+Argentina") ."&oe=utf8&key=abcdefg");
$b = json_decode($b, TRUE);
list($longitude,$latitude) = $b['Placemark'][0]['Point']['coordinates'];
echo "Longitude: " . $longitude . "<br />Latitude: " . $latitude;

请注意,在这里,我们假设我们只返回一个地标,如果您期望更多,则循环 $b['Placemark'] 变量以获取您的数据。

【讨论】:

  • 嗨,我试过了,但我收到了警告,警告:file_get_contents(maps.google.com/maps/geo?q=1050+BRAVOMario,+Capital Federal,+Argentina&oe=utf8&key=MYKEEYYY!!你有什么线索吗?
猜你喜欢
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多