2013-08-31 153 views
0

我正在通过gmap3 plugin谷歌地图。在这里我已经从谷歌地图代码中获得longitude value and latitude value。现在我想要这个值的地址,如country name state name and city name etc。那么如何从longitude and latitude值中获得这些值。我的代码看起来像这样谷歌地图得到经度和纬度值的总地址

jQuery(document).ready(function() { 
    jQuery('#map').blur(function() { 
    //console.log('hello'); 
    var the_address = jQuery(this).val(); 
    var geocoder = new google.maps.Geocoder(); 
    var latLng = geocoder.geocode({ 
     address: the_address 
    }, function(results, status) { 

    /// IF WE HAVE A RESULT 
    if(status == google.maps.GeocoderStatus.OK) { 
    lat = results[0].geometry.location.lat(); 
    lng = results[0].geometry.location.lng(); 
    jQuery('#latitude').val(lat); 
    jQuery('#longitude').val(lng); 

    jQuery(gMap).gmap3({ 
     get: { 
     name: 'marker', 
     all: true, 
     callback: function(objs) { 
      jQuery.each(objs, function(i, obj) { 
      obj.setMap(null); 
      }) 
     } 
     }, 
     map: { 
     options: { 
      zoom: 10, 
      center: new google.maps.LatLng(lat, lng) 
     } 
     }, 
     marker: { 
      values: [{ latLng:[lat, lng] }], 
      //jQuery(console.log(values)); 
      options: { 
      draggable: true, 
      icon: new google.maps.MarkerImage(
      "http://gmap3.net/skin/gmap/magicshow.png", 
      new google.maps.Size(32, 37, "px", "px") 
      ), 
      }, 
      events: { 
      mouseup: function(marker, event, context) { 
       //// GETS MARKER LATITUDE AND LONGITUDE 
       var thePos = marker.getPosition(); 
       var theLat = thePos.jb; 
       var theLng = thePos.kb; 
       jQuery('#latitude').val(theLat); 
       jQuery('#longitude').val(theLng); 
      }, 
      dragend: function(marker, event, context) { 
      jQuery(this).gmap3({ 
       getaddress:{ 
       latLng:marker.getPosition(), 
       callback:function(results){ 
        var map = $(this).gmap3("get"), 
        infowindow = $(this).gmap3({get:"infowindow"}), 
        content = results && results[1] ? results && results[1].formatted_address : "no address"; 
        if (infowindow){ 
        infowindow.open(map, marker); 
        infowindow.setContent(content); 
        } else { 
        jQuery(this).gmap3({ 
        infowindow:{ 
        anchor:marker, 
        options:{content: content} 
        } 
       }); 
       } 
      } 
      } 
     }); 
      }, 

     } 
     } 
    }); 
     } else { 
     alert('Could not find the latitude and longitude for the address '+the_address); 
       } 
     }); 
    }); 
}); 

任何帮助和建议都将非常可观。谢谢

+0

你想去哪里得到的?您只需使用Lat和Lng反转地理代码即可获取完整地址。 – putvande

回答

0

我不确定你想要哪个完整地址,但很容易找到它。如果您想从Geocoder的结果中获得,只需致电results[0].formatted_address即可获得完整地址。

如果你只想在你可以做一个反向地理编码lat和长值填充,以获得地址:

var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'latLng': new google.maps.LatLng(lat,lon) }, function(results, status){ 
    console.log(results[0].formatted_address); 
}); 

https://developers.google.com/maps/documentation/javascript/geocoding?hl=nl#ReverseGeocoding