Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取任何地址的经度和纬度?_Php - Fatal编程技术网

Php 如何获取任何地址的经度和纬度?

Php 如何获取任何地址的经度和纬度?,php,Php,您好,如果我有PHP中的街道名称、州名称、城市名称、国家名称和邮政编码,那么获取任何地址的经度和纬度的公式是什么? 谢谢您需要访问地理编码服务(即从谷歌),没有简单的公式可以将地址转换为地理坐标 您可以使用谷歌地图API来实现这一点。有关更多信息,请参阅下面的博客文章 由于街道名称和城市基本上是随机发放的,因此没有统一的标准。需要在数据库中查找地址。或者,您可以在数据库中查找邮政编码所在地区的邮政编码 你没有提到一个国家,所以我假设你只想要美国的地址。你可以使用很多数据库,有些是免费的,有些不是

您好,如果我有PHP中的街道名称、州名称、城市名称、国家名称和邮政编码,那么获取任何地址的经度和纬度的公式是什么?
谢谢

您需要访问地理编码服务(即从谷歌),没有简单的公式可以将地址转换为地理坐标

您可以使用谷歌地图API来实现这一点。有关更多信息,请参阅下面的博客文章


由于街道名称和城市基本上是随机发放的,因此没有统一的标准。需要在数据库中查找地址。或者,您可以在数据库中查找邮政编码所在地区的邮政编码

你没有提到一个国家,所以我假设你只想要美国的地址。你可以使用很多数据库,有些是免费的,有些不是


您还可以使用谷歌地图API让他们在数据库中为您查找地址。这可能是最简单的解决方案,但要求您的应用程序始终具有工作的internet连接。

使用以下代码使用php获取lat和long。以下是两种方法:

第一类:

编辑-谷歌地图请求必须通过https

第2类:

var地理编码器;
var映射;
函数初始化(){
geocoder=新的google.maps.geocoder();
var latlng=新的google.maps.latlng(50.804400,-1.147250);
变量映射选项={
缩放:6,
中心:拉丁
}
map=new google.maps.map(document.getElementById('map-canvas12'),mapOptions);
}
功能代码地址(地址、图名、url、距离、价格、邮政编码){
var地址=地址;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
var marker=new google.maps.marker({
地图:地图,
位置:结果[0]。几何体。位置
});
var infowindow=new google.maps.infowindow({
内容:“导师姓名:”+tutorname+”
价格指南:“+prise+”
距离:“+Distance+”距离您(“+postcode+”)
' }); 信息窗口。打开(地图、标记); }/*其他{ 警报('地理编码因以下原因未成功:'+状态); }*/ }); } google.maps.event.addDomListener(窗口“加载”,初始化); window.onload=函数(){ 初始化(); //你的代码在这里 代码地址(“”,“”,“”,“”,“”,“”); };
PHP有一些很好的内置函数来获取地理位置。也许你可以看看这里:


根据php手册,“此扩展需要安装GeoIP C库版本1.4.0或更高版本。您可以从»获取最新版本并自行编译。”

考虑到传入的垃圾和文件内容失败,我提出了以下建议

function get_lonlat(  $addr  ) {
    try {
            $coordinates = @file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($addr) . '&sensor=true');
            $e=json_decode($coordinates);
            // call to google api failed so has ZERO_RESULTS -- i.e. rubbish address...
            if ( isset($e->status)) { if ( $e->status == 'ZERO_RESULTS' ) {echo '1:'; $err_res=true; } else {echo '2:'; $err_res=false; } } else { echo '3:'; $err_res=false; }
            // $coordinates is false if file_get_contents has failed so create a blank array with Longitude/Latitude.
            if ( $coordinates == false   ||  $err_res ==  true  ) {
                $a = array( 'lat'=>0,'lng'=>0);
                $coordinates  = new stdClass();
                foreach (  $a  as $key => $value)
                {
                    $coordinates->$key = $value;
                }
            } else {
                // call to google ok so just return longitude/latitude.
                $coordinates = $e;

                $coordinates  =  $coordinates->results[0]->geometry->location;
            }

            return $coordinates;
    }
    catch (Exception $e) {
    }
然后,要获取跳线: 其中$pc是邮政编码或地址。。。。 $address=get_lonlat($pc); $l1=$address->lat; $l2=$address->lng


<?php
$address = 'BTM 2nd Stage, Bengaluru, Karnataka 560076'; // Address
$apiKey = 'api-key'; // Google maps now requires an API key.
// Get JSON results from this request
$geo = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false&key='.$apiKey);
$geo = json_decode($geo, true); // Convert the JSON to an array

if (isset($geo['status']) && ($geo['status'] == 'OK')) {
  $latitude = $geo['results'][0]['geometry']['location']['lat']; // Latitude
  $longitude = $geo['results'][0]['geometry']['location']['lng']; // Longitude
}
?>

Hi Ajay-我想你指的是“经度”可能是重复的。澄清一下,没有公式,但公司确实提供地理编码服务。请看上面链接的“dupe”。这正是我要找的。非常感谢。我能问点事吗?我只需要为内部计算使用此功能。API文档明确指出,API只能与Google地图服务结合使用。您的代码示例是否处于这种情况?在类型1中,我得到“您已超过此API的每日请求配额”。在一些测试之后,出现错误消息。。。没有获得每日请求配额的任何解决方法?在上面的PHP示例中,需要在URL的末尾添加“&key=API\u key”,否则您可能会遇到超出配额的问题。e、 g.这里有更多信息:确保在php版本的
文件获取内容行中添加
https://
,否则它将返回
Array([error\u message]=>对此API的请求必须通过SSL。加载API时使用“https://”而不是“http://”。[results]=>Array()[status]=>REQUEST\u DENIED)NULL
不会使用urlencode()最好准备好地址,因为“&”会把一切都搞糟,而不是仅仅用“+”替换空格。对于其他人来说,如果无法检索到它们,请打印$geo变量以查看错误的发生。对我来说,我必须使用一个API密钥,在查询中用(&key=[API_key])和https指定。@DirtyBit:你让我很尴尬I’很高兴再次见到你。