使用Google地图API和PHP进行反向地理编码,以使用Lat,Long坐标获取最近的位置 [英] Reverse Geocoding With Google Map API And PHP To Get Nearest Location Using Lat,Long coordinates

查看:150
本文介绍了使用Google地图API和PHP进行反向地理编码,以使用Lat,Long坐标获取最近的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,使用google map api,反向地理编码和php来获得离坐标(lat,long)最近的地址或城市...请给出一些示例代码

解决方案

您需要使用 GClientGeocoder 上的noreferrer> getLocations 方法对象位于 Google Maps API

  var point = new GLatLng(43,-75); 
var geocoder = new GClientGeocoder();
geocoder.getLocations(point,function(result){
//从地标对象中获取地址
alert(result.address);
});

编辑:好的。你正在做这东西服务器端。这意味着您需要使用 HTTP地址解析服务 。为此,您需要使用链接文章中描述的URL格式发出HTTP请求。您可以解析HTTP响应并提取地址:

pre code $ //在这里设置您的API密钥
$ api_key = ;
//用适当的纬度经度格式化此字符串
$ url ='http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&sensor= true_or_false& key ='。 $ API_KEY;
//发出HTTP请求
$ data = @file_get_contents($ url);
//解析json响应
$ jsondata = json_decode($ data,true);
//如果我们得到地标数组并且状态良好,则获取地址
if(is_array($ jsondata)&& $ jsondata ['Status'] ['code'] == 200)
{
$ addr = $ jsondata ['Placemark'] [0] ['address'];
}

注意 Google地图服务条款明确规定,在不将结果放到Google上的情况下对地理数据进行地理编码地图是禁止的。


I need a function to get an nearest address or city from coordinates(lat,long) using google map api reverse geocoding and php... Please give some sample code

解决方案

You need to use the getLocations method on the GClientGeocoder object in the Google Maps API

var point = new GLatLng (43,-75);
var geocoder = new GClientGeocoder();
geocoder.getLocations (point, function(result) {
    // access the address from the placemarks object
    alert (result.address);
    });

EDIT: Ok. You are doing this stuff server side. This means you need to use the HTTP Geocoding service. To do this you will need to make an HTTP request using the URL format described in the linked article. You can parse the HTTP response and pull out the address:

// set your API key here
$api_key = "";
// format this string with the appropriate latitude longitude
$url = 'http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&sensor=true_or_false&key=' . $api_key;
// make the HTTP request
$data = @file_get_contents($url);
// parse the json response
$jsondata = json_decode($data,true);
// if we get a placemark array and the status was good, get the addres
if(is_array($jsondata )&& $jsondata ['Status']['code']==200)
{
      $addr = $jsondata ['Placemark'][0]['address'];
}

N.B. The Google Maps terms of service explicitly states that geocoding data without putting the results on a Google Map is prohibited.

这篇关于使用Google地图API和PHP进行反向地理编码,以使用Lat,Long坐标获取最近的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆