Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.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
Javascript 在城市名称代码中启用地理定位';好像不行_Javascript_Html_Google Maps_Geolocation - Fatal编程技术网

Javascript 在城市名称代码中启用地理定位';好像不行

Javascript 在城市名称代码中启用地理定位';好像不行,javascript,html,google-maps,geolocation,Javascript,Html,Google Maps,Geolocation,我正在尝试获取地理位置并将其转换为文本。我有代码来做,但它给了我一个错误。如何解决这个错误 错误:TypeError:“undefined”不是对象(正在评估“geocoder.geocode”) 守则: var geocoder; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(successFunction, errorFunction); } //Get the latitude and the

我正在尝试获取地理位置并将其转换为文本。我有代码来做,但它给了我一个错误。如何解决这个错误

错误:TypeError:“undefined”不是对象(正在评估“geocoder.geocode”)

守则:

var geocoder;

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} 
//Get the latitude and the longitude;
 function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}

function errorFunction(){
alert("Geocoder failed");
}

function initialize() {
geocoder = new google.maps.Geocoder();



}

function codeLatLng(lat, lng) {

var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'location':latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
  console.log(results)
    if (results[1]) {
     //formatted address
     alert(results[0].formatted_address)
    //find country name
         for (var i=0; i<results[0].address_components.length; i++) {
        for (var b=0;b<results[0].address_components[i].types.length;b++) {

        //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
            if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
                //this is the object you are looking for
                city= results[0].address_components[i];
                break;
            }
        }
    }
    //city data
    alert(city.short_name + " " + city.long_name)


    } else {
      alert("No results found");
    }
  } else {
    alert("Geocoder failed due to: " + status);
  }
});
}
var地理编码器;
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(successFunction,errorFunction);
} 
//得到纬度和经度;
功能成功功能(位置){
var lat=位置坐标纬度;
var lng=位置坐标经度;
codeLatLng(lat,lng)
}
函数errorFunction(){
警报(“地理编码器失败”);
}
函数初始化(){
geocoder=新的google.maps.geocoder();
}
功能代码LATLNG(lat,lng){
var latlng=新的google.maps.latlng(lat,lng);
geocoder.geocode({'location':latlng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
console.log(结果)
如果(结果[1]){
//格式化地址
警报(结果[0]。格式化的\u地址)
//查找国家名称
对于(var i=0;iTo),请求结构包含
latLng
,而不是
location

根据文档中的示例稍微修改(未测试):

此外,这是不正确的,如果要使用第一个结果(结果[0]),则应检查该结果是否存在(如果结果[0]…而不是如果结果[1]):


查看,您没有正确加载API。请查看

如果错误在线显示为
geocoder.geocode
,请检查latlng变量是否确实分配了latlng对象。我检查了,这似乎不是问题。感谢您的回答:)不过我还是遇到了同样的错误。未捕获的TypeError:无法调用undefined的方法“geocode”在您尝试使用geocoder之前,您的初始化函数被调用了?我检查了,情况确实如此。但是,现在我得到了一个不同的错误:TypeError:“undefined”不是对象(评估“geocoder.geocode”)我没有发现任何错误。也许你需要发布更多的代码或者一个JSFIDLE来显示问题。这个JSFIDLE会显示问题吗?
 function codeLatLng(lat, lng) {
    // remove irrelevant code
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
 //formatted address
 alert(results[0].formatted_address)