2012-12-14 78 views
0

我有一個XML文件,其中包含氣象測量的緯度和經度。 我想用Javascript找到與給定經度和緯度對應的城市。我應該怎麼做 ?Javascript:使用緯度/經度查找城市?

謝謝您的回答

+0

沒錯,這就是你所需要的。 https://developers.google.com/maps/documentation/geocoding/index – elclanrs

+0

我會看看,謝謝。 – Rob

回答

1
// To initialisation Map and GeoCoder 
var geocoder = new google.maps.Geocoder(); 
var infowindow = new google.maps.InfoWindow(); 
var marker,map; 
var directionsDisplay; 
var directionsService = new google.maps.DirectionsService(); 

directionsDisplay = new google.maps.DirectionsRenderer(); 
      var chicago = new google.maps.LatLng(41.850033, -87.6500523); 
      var mapOptions = { 
       zoom: 6, 
       mapTypeId: google.maps.MapTypeId.ROADMAP, 
       center: chicago 
      } 

map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

// It is called Reverse Geocoding 

     var latlng = new google.maps.LatLng(41.850033, -87.6500523); 
     geocoder.geocode({'latLng': latlng}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
      if (results[1]) { 
       map.setZoom(11); 
       marker = new google.maps.Marker({ 
        position: latlng, 
        map: map 
       }); 
       infowindow.setContent(results[1].formatted_address); 
       infowindow.open(map, marker); 
      } else { 
       alert('No results found'); 
      } 
      } else { 
      alert('Geocoder failed due to: ' + status); 
      } 
相關問題