2016-06-07 100 views
0

我想通过javascript中的Google Maps Geolocation API在MAC地址的基础上获取物理位置(经纬度和长度)。Google Maps API:请求和接收json数据以获取地理位置

按照谷歌地图API的文档,进行地理定位,信息必须在要求其所返回JSON数据https://developers.google.com/maps/documentation/geolocation/intro#responses

我已经试过这段JavaScript代码的指定网址JSON格式https://developers.google.com/maps/documentation/geolocation/intro#body

xhr = new XMLHttpRequest(); 
var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY"; 
xhr.open("POST", url, true); 
xhr.setRequestHeader("Content-type", "application/json"); 
xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4 && xhr.status == 200) { 
     var json = JSON.parse(xhr.responseText); 
     console.log(json.location.lat+","+json.location.lng+","+json.accuracy); 
    } 
} 
var data = JSON.stringify({ 
       "wifiAccessPoints": [ 
        { 
         "macAddress": "MY_MAC_ADDRESS" 
        }, 
        { 
         "macAddress": "MY_MAC_ADDRESS" 
        } 
       ] 
      }); 

xhr.send(data); 

但我得到以下错误控制台

POST https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY 403() 

什么代码错了?或者还有另一种更好的方法?

NOTE:您可能会问为什么不简单使用navigator.geolocation?那么,因为我构建项目的系统不支持HTML5地理定位API。

回答

0

错误(403),你得到一个基于documentiondailyLimitExceededuserRateLimitExceeded。原因是您已超出您在Google Developers Console中配置的每用户每用户限制的请求数。应该配置此限制以防止单个或一小组用户耗尽您的每日配额,同时仍允许合理访问所有用户。

有关更多信息以及如何检查您在Google Developers Console中使用的使用情况或配额,请遵循此page上的指导原则。