簡體   English   中英

Google Maps JavaScript API V3-Geocoder API未返回地名

[英]Google Maps JavaScript API V3 - Geocoder API is not returning place name

這是我的代碼,我是Google Maps和JavaScript的新手,我正在嘗試通過將位置(latlng)傳遞給地址解析器來獲取“地名”,但未返回任何值。 如果將placeName var設置為“ 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
}

您的代碼中有語法錯誤。

  1. 您需要使用有效的GeocoderRequest對象調用google.maps.Geocoder geocode方法:
var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    location: location
  }, function(results, status) 
  1. Geocoder是異步的,因此您需要在可用的時間/位置在回調函數中使用返回的結果:
  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);

概念證明

代碼段:

 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> 

只需嘗試將lat和long傳遞給以下查詢字符串,您將獲得一個json數組,從那里獲取您的地名。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM