Google Maps JavaScript API V3-地理编码器API未返回地名

Google Maps JavaScript API V3-地理编码器API未返回地名,javascript,google-maps-api-3,google-maps-markers,reverse-geocoding,google-geocoding-api,Javascript,Google Maps Api 3,Google Maps Markers,Reverse Geocoding,Google Geocoding Api,这是我的代码,我是谷歌地图和JavaScript的新手,我试图通过将位置(latlng)传递给地理编码器来获取“地名”,但它不会返回任何值。如果将placeName变量设置为“somePlace”,则将标题设置为“somePlace”。但我想用返回值设置标题,即地名 function setMap(position){ //var Karachi = {lat: 24.8978176, lng: 66.9596003}; //default city var location = {

这是我的代码,我是谷歌地图和JavaScript的新手,我试图通过将位置(latlng)传递给地理编码器来获取“地名”,但它不会返回任何值。如果将placeName变量设置为“somePlace”,则将标题设置为“somePlace”。但我想用返回值设置标题,即地名

function setMap(position){
  //var Karachi = {lat: 24.8978176, lng: 66.9596003}; //default city
  var location = {
    lat: position.coords.latitude,
    lng: position.coords.longitude
  }

  var mapOptions = {

      center: location,
      zoom: 13,         //initial zoom level
      mapTypeId: 'hybrid'  //map type
  }
  //object of map
  var map = new google.maps.Map(document.getElementById('map'), mapOptions);

  var placeName;
  //placeName = 'SomePlace'; 
  var newLatLng = location + '';
  var latlngStr = newLatLng.split(',',2);
  var latlng = {lat: parseFloat(latlngStr[0]), lng:    parseFloat(latlngStr[1])};
  var geocoder = new google.maps.Geocoder({'location': latlng}, function(results, status) {
    if(status === 'OK'){
      if(results[1]){
        placeName = 'results[1].formatted_address';
      }
      else{
        window.alert('Error in ' + status);
      }
    }
    else{
      window.alert('Geocoder failed due to ' + status);
    }
  });

  var markerOptions = {
    position: location,
    map: map,
    title: placeName //<-- i want place name to be here

  }
  //object of marker
  var marker = new google.maps.Marker(markerOptions);
  marker.setTitle(placeName); //<-- this is also not working
}
功能设置图(位置){
//var Karachi={lat:24.8978176,lng:66.9596003};//默认城市
变量位置={
纬度:位置坐标纬度,
lng:position.coords.longitude
}
变量映射选项={
中心:位置,
缩放:13,//初始缩放级别
mapTypeId:'混合'//映射类型
}
//地图对象
var map=new google.maps.map(document.getElementById('map'),mapOptions);
变量地名;
//placeName='SomePlace';
var newLatLng=位置+“”;
var latlngStr=newLatLng.split(',',2);
var-latlng={lat:parseFloat(latlngStr[0]),lng:parseFloat(latlngStr[1]);
var geocoder=new google.maps.geocoder({'location':latlng},函数(结果,状态){
如果(状态=='OK'){
如果(结果[1]){
placeName='结果[1]。格式化的_地址';
}
否则{
window.alert(状态为“+时出错);
}
}
否则{
window.alert('地理编码器由于'+状态而失败);
}
});
变量标记选项={
位置:位置,,
地图:地图,

title:placeName/只需将lat和long传递给以下查询字符串,您将获得一个json数组,从中获取您的placeName

http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true

代码中有语法错误

  • 您需要使用有效对象调用
    geocode
    方法:
  • 地理编码器是异步的,您需要在回调函数中使用返回的结果(当/在可用时):
  • html,
    身体
    #地图{
    身高:100%;
    宽度:100%;
    边际:0px;
    填充:0px
    }
    
    
    我尝试过这个,但它返回了“零结果”,现在不知道该怎么办。你尝试过在浏览器中粘贴这个url吗?我使用了你提供的链接,它工作正常,它给了我一个json对象,但当我尝试传递地理定位标签时,它给出了零结果,我还尝试了浏览器传感器,并将位置设置为伦敦和孟买,但没有成功没有任何结果。
    var geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        location: location
      }, function(results, status) 
    
      if (results[1]) {
        placeName = results[1].formatted_address;
        var markerOptions = {
            position: location,
            map: map,
            title: placeName //<-- i want place name to be here
    
          }
          //object of marker
        var marker = new google.maps.Marker(markerOptions);