簡體   English   中英

Google地圖從經度和緯度值獲取總地址

[英]Google map get the total address from longitude and latitude values

我正在通過Google Map的gmap3插件 在這里,我從Google地圖代碼中獲得了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);
              }
      });
    });
});

任何幫助和建議都是非常可取的。 謝謝

我不確定您想在哪里獲得完整的地址,但是很容易得到它。 如果您希望從Geocoder的結果中獲取它,則只需調用results[0].formatted_address即可獲取完整地址。

如果您只想填寫lat和long值即可獲取地址,則可以執行反向地址解析:

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

暫無
暫無

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

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