-1

这是我的代码,我是新的谷歌地图和JavaScript,我试图通过传递位置(latlng)到'地名'到geocoder,但它没有返回任何值。如果将placeName var设置为'somePlace',它将标题设置为'somePlace'。但我想用返回值设置标题,例如地名。谷歌地图JavaScript API V3 - 地理编码API不返回地名

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 
} 
+0

则可以反向地理编码使用地理编码工具的坐标? https://google-developers.appspot.com/maps/documentation/utils/geocoder/ – xomena

回答

0

您的代码中有语法错误。

  1. 你需要调用google.maps.Geocodergeocode方法,使用有效的GeocoderRequest对象:
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); 
    

    proof of concept fiddle

    代码片段:

    function setMap(position) { 
     
        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 geocoder = new google.maps.Geocoder(); 
     
        geocoder.geocode({ 
     
        location: location 
     
        }, function(results, status) { 
     
        if (status === 'OK') { 
     
         if (results[1]) { 
     
         placeName = results[1].formatted_address; 
     
         var markerOptions = { 
     
          position: location, 
     
          map: map, 
     
          title: placeName // set formatted_address as title of marker 
     
         } 
     
         //object of marker 
     
         var marker = new google.maps.Marker(markerOptions); 
     
         } else { 
     
         window.alert('Error in ' + status); 
     
         } 
     
        } else { 
     
         window.alert('Geocoder failed due to ' + status); 
     
        } 
     
        }); 
     
    } 
     
    google.maps.event.addDomListener(window, "load", function() { 
     
        setMap({ 
     
        coords: { 
     
         latitude: 37.4419, 
     
         longitude: -122.1419 
     
        } 
     
        }) 
     
    });
    html, 
     
    body, 
     
    #map { 
     
        height: 100%; 
     
        width: 100%; 
     
        margin: 0px; 
     
        padding: 0px 
     
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script> 
     
    <div id="map"></div>

    +0

    非常感谢您,我错过了地理编码功能,再次感谢您为我节省开支,快乐的编码! –

    0

    只需将您的lat和long传递给下面的查询字符串,然后您将获得一个json数组,从那里获取您的地名。

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

    我试过这个,但是它返回'Zero_Results',不知道现在该做什么。 –

    +0

    您是否尝试过在浏览器中粘贴这个网址? –

    +0

    我用你提供的链接,它工作正常,它给了我一个JSON对象,但是当我试图传递位于地理位置的latlng时,它给了zero_results,我也尝试了浏览器传感器并将位置设置到伦敦和孟买,但未能得到任何结果。 –