【问题标题】:Get my current address using javascript使用 javascript 获取我当前的地址
【发布时间】:2013-01-12 21:27:34
【问题描述】:

我对使用 Javascript 获取当前地址很感兴趣,只是通过组装其他一些 SO 线程 (1,2) 解决了这个问题,所以想发布这个问题和答案。

请看下面的答案。

【问题讨论】:

  • 它给了我不正确的位置但有用的东西:D
  • @AspiringAqib 很遗憾听到它不适合您。我在美国,它给了我确切的地址。它为您提供的位置是否离您居住的地方很近?
  • 对我来说,它为我提供了一个距离我所在位置 3 公里的位置(在台式电脑上)。不过,对于移动设备来说还是不错的,因为它们有 GPS 来获取坐标。
  • @Cerbrus,是的,还不错。酷!
  • @AspiringAqib 可能会更好,虽然很高兴城市和国家是正确的。

标签: javascript jquery google-maps geolocation


【解决方案1】:

这是 HTML:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<p id='latitudeAndLongitude'></p>
<p id='address'></p> 

这是 JS:

var latitudeAndLongitude=document.getElementById("latitudeAndLongitude"),
location={
    latitude:'',
    longitude:''
};

if (navigator.geolocation){
  navigator.geolocation.getCurrentPosition(showPosition);
}
else{
  latitudeAndLongitude.innerHTML="Geolocation is not supported by this browser.";
}

function showPosition(position){ 
    location.latitude=position.coords.latitude;
    location.longitude=position.coords.longitude;
    latitudeAndLongitude.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
    var geocoder = new google.maps.Geocoder();
    var latLng = new google.maps.LatLng(location.latitude, location.longitude);

 if (geocoder) {
    geocoder.geocode({ 'latLng': latLng}, function (results, status) {
       if (status == google.maps.GeocoderStatus.OK) {
         console.log(results[0].formatted_address); 
         $('#address').html('Address:'+results[0].formatted_address);
       }
       else {
        $('#address').html('Geocoding failed: '+status);
        console.log("Geocoding failed: " + status);
       }
    }); //geocoder.geocode()
  }      
} //showPosition

【讨论】:

  • 如果用户使用没有 GPS 的设备,例如 PC,该位置很可能是 IP 地址的位置。在我的情况下,该位置在 7 英里外。HTML5 Documentation
  • @davidstrachan 谢谢你知道这真的很好,似乎解释了上面的 -@Cerbus 和 -@Aspiring 的 cmets。
  • 干得好@timpeterson,效果很好。只需对其稍作改动......按原样使用它,它会在页面加载时重定向我的浏览器。所以我删除了 ',location = { ... };'部分代码作为 Javascript 中的位置重定向您的浏览器。在此之后,它工作得非常好。
【解决方案2】:

使用HTML5 GeoLocation:

function GetGeolocation() {

navigator.geolocation.getCurrentPosition(GetCoords, GetError);

}


function GetCoords(position){

  alert(position.coords.latitude);

  alert(position.coords.longitude);

  alert(position.coords.accuracy);

 var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
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("Geocoder failed due to: " + status);
  }
});

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 2012-03-17
    • 2021-08-31
    • 1970-01-01
    • 2013-04-29
    • 2011-06-29
    相关资源
    最近更新 更多