-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,我也嘗試了瀏覽器傳感器並將位置設置到倫敦和孟買,但未能得到任何結果。 –