使用JavaScript获取纬度/经度值时获取MissingKeyMapError

时间:2016-06-30 07:48:02

标签: javascript google-maps

尝试使用JavaScript从地址获取纬度和经度值时出现以下错误。

错误:

  

Google Maps API错误:MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error

我正在解释下面的代码。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<div>
     <h3> Enter an adress and press the button</h3>

    <input id="address" type="text" placeholder="Enter address here" />
    <button id="btn">Get LatLong</button>
    <div>
        <p>Latitude:
            <input type="text" id="latitude" readonly />
        </p>
        <p>Longitude:
            <input type="text" id="longitude" readonly />
        </p>
    </div>
</div>
<script>
function getLatitudeLongitude(address,callback) {
    // If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
    address = address || 'Ferrol, Galicia, Spain';
    // Initialize the Geocoder
    geocoder = new google.maps.Geocoder();
    if (geocoder) {
        geocoder.geocode({
            'address': address
        }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                callback(results[0]);
            }
        });
    }
}
document.getElementById('btn').click=function(){
    var address = document.getElementById('address').value;
    getLatitudeLongitude(address,function(result){
         console.log('result',result);
         document.getElementById('latitude').value = result.geometry.location.lat();
         document.getElementById('longitude').value = result.geometry.location.lng();
    });
}
</script>

我在控制台中收到了这些错误。这里我在localhost中运行代码。

1 个答案:

答案 0 :(得分:2)

您的代码在不在localhost上运行时有效。

谷歌搜索了一下我发现了here(和官方公告http://googlegeodevelopers.blogspot.com.au/2016/06/building-for-scale-updates-to-google.html

  

如果您在localhost上使用Google Maps API,或者您的域名是   在2016年6月22日之前没有活动,它将需要一个密钥   向前。要解决此问题,请参阅Google Maps API   获取密钥并将其添加到应用程序的文档:   https://developers.google.com/maps/documentation/javascript/get-api-key

    this._server = config.GetAttribute("server");
    **this._workspace = config.GetAttribute("workspace");**
    this._user = config.GetAttribute("user");
    this._password = config.GetAttribute("psw");
    TeamFoundationServer tfs = new TeamFoundationServer(this._server, new System.Net.NetworkCredential(this._user, this._password));
    tfs.Authenticate();
    VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
    Workspace ws = versionControl.GetWorkspace(this._workspace, this._user);
function getLatitudeLongitude(address, callback) {
  // If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
  address = address || 'Ferrol, Galicia, Spain';
  // Initialize the Geocoder
  geocoder = new google.maps.Geocoder();
  if (geocoder) {
    geocoder.geocode({
      'address': address
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        callback(results[0]);
      }
    });
  }
};

function getLatLon() {
  var address = document.getElementById('address').value;
  getLatitudeLongitude(address, function(result) {
    //console.log('result',result);
    document.getElementById('latitude').value = result.geometry.location.lat();
    document.getElementById('longitude').value = result.geometry.location.lng();
  });
}