php从没有api密钥的地址获取纬度 [英] php get latitude from address without api key

查看:59
本文介绍了php从没有api密钥的地址获取纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有获取地址并返回纬度和经度的功能.

I have function that get address and return latitude and longitude.

问题在于,过了一会儿,我的经纬度均达到0.00000lng.我想我需要在网址中添加& key = API_KEY",如果我理解正确的话,那是花钱的.

The problem is that after a while i'm getting 0.00000 for both lat & lng. I guess that i need to add "&key=API_KEY" to the url, and if i got it right it's cost money.

还有其他方法可以获取地址lat&吗?lng吗?

Is there any other way to get address lat & lng ?

function get_address_lat_lng ($address)
{

    $prepAddr = str_replace(' ','+',$address);

    $geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.urlencode($prepAddr).'&sensor=false');

    $output= json_decode($geocode);

    $lat = $output->results[0]->geometry->location->lat;
    $lng = $output->results[0]->geometry->location->lng;

    return (array('lat' => $lat, 'lng' => $lng) );
}

推荐答案

function ipLookUp () {
  $.ajax('http://ip-api.com/json')
   .then(
    function success(response) {
      console.log('User\'s Location Data is ', response);
      console.log('User\'s Country ', response.country);
    },

    function fail(data, status) {
      console.log('Request failed.  Returned status of ', status);
    }
  );
}
ipLookUp()

在浏览器上检查控制台日志,但这基于客户端的IP.

Check your console logs on browsers but this is based on IP of client.

这篇关于php从没有api密钥的地址获取纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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