duanbu9345 2016-09-15 12:32
浏览 109
已采纳

从Google Geolocation API获取当前的lat和lng

I have no problem getting the current lat and lng using javascript. But I would like to call the API link in PHP via file_get_contents as following:

<?php
$json = file_get_contents('https://www.googleapis.com/geolocation/v1/geolocate?key=Jsddds6s6sdht43asdsaASasta8962');
?>

Current if I paste the URL in Chrome, I am getting Not Found. Why does this happened?

  • 写回答

2条回答 默认 最新

  • dongzhao1865 2016-09-15 13:14
    关注

    Its giving error because your are use query string GET method for it

    use POST method to retrieve data because google geolocation only allows post method enter image description here here is an example

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Geocode  Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
     
    </head>
    <body>
    <span>Your Latitude : </span><span id="lat"></span><br>
    <span>Your Longitude : </span><span id="lng"></span><br>
    <button id="btn">Click Here To Get Lat And Lng</button>
    <script>
    $(document).ready(function() {
        $('#btn').click(function(e) {
             $.ajax({
            type : 'POST',
            data: '', 
            url: "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyCW0lvagDP67ulkwwP7yAIBHJoj2HT0apM", 
            success: function(result){
                    $('#lat').html(result['location']['lat']);
                    $('#lng').html(result['location']['lng']);
                        
                }});
                        
        });
    } );
    </script>
    
    </body>
    </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100