谷歌地图 - 当前位置

时间:2010-03-20 09:03:58

标签: javascript geolocation google-maps

我想获取当前用户位置的地址字符串。这可能吗?

非常感谢。 此致

5 个答案:

答案 0 :(得分:2)

用户的位置在google.loader.ClientLocation中可用(如果已知IP,则将包含基于客户端IP的猜测)

  

google.loader.ClientLocation

     

当应用程序使用时   AJAX API加载器,加载器尝试   根据地理位置定位客户端   IP地址。如果这个过程成功,   客户的位置,范围到   地铁一级,在   google.loader.ClientLocation属性。   如果进程找不到匹配项,   此属性设置为null。

http://code.google.com/apis/ajax/documentation/#ClientLocation

答案 1 :(得分:1)

如果您需要比IP地理位置更精确的内容,您可以在支持的浏览器(特别是Firefox 3.5和Mobile Safari)中使用W3C Geolocation API来请求用户的纬度和经度,然后使用Google's client-side reverse geocoding service猜猜用户的大概街道地址。 (my test page上包含的这两个步骤的代码示例。)

如果你走这条路,请务必事先向你的用户解释你为什么要求他们的位置以及你如何使用他们的信息(例如你在实际提交表格之前没有存储它),因为它在技术上是由API所要求的,因为一些用户会被你猜测他们的街道地址变得容易。

答案 2 :(得分:1)

如果你想获得街道地址

包括 JS MAP Api Key,keep all together from the wizard

我的代码看起来像这样:(也使用一点jQuery)

var geocoder = null;
var lat = null;
var lng = null;

$(function() {
   if (GBrowserIsCompatible()) {

 if (google.loader.ClientLocation &&
     // i used this here cause not always 
     //this retrieve the correct LAT & LON
     // so set it manually
      google.loader.ClientLocation.address.country_code == "IT" ) {
      lat = google.loader.ClientLocation.latitude;
      lng = google.loader.ClientLocation.longitude;
      } else {
      //alert( 'Insert Your Latitude & longitude manually!' );
      lat = '42.464826';
      lng = '14.214095';
      }
    //creat the full LAT + LON
    points = new GLatLng( lat , lng ); 
    //get the Street Address
    get_address(points);
    }
 });

    function get_address(latlng) {
      if (latlng) {
      geocoder = new GClientGeocoder();
        geocoder.getLocations(latlng, function(addresses) {
          if(addresses.Status.code != 200) {
            alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
          }
          else {
            address = addresses.Placemark[0];
            var myHtml = address.address;

            $('#address').text(myHtml);

          }
        });
      }
    };
peraphs阅读此链接也可能有所帮助:

  

Get street address at lat/long pair

答案 3 :(得分:1)

是的,有可能实现使用html 5和Google API 3,在给定代码中你必须更改你的APIKey并且map-canvas是html正文中的一个元素

  <script type="text/javascript"
     src="https://maps.googleapis.com/maps/api/js?key=yourAPIKey&sensor=false"></script>
  <script type="text/javascript">
     var lat;
     var lng;

         function initialize() {
      if (navigator.geolocation)
        {
        navigator.geolocation.getCurrentPosition(showPosition);
        }       


         }
     function showPosition(position)
      {
      lat = position.coords.latitude; 
      lng = position.coords.longitude;
     var myLatlng = new google.maps.LatLng(lat, lng);

     var myTittle = "My Current Location! Lat: " + lat + "lng: " + lng;
     var marker = new google.maps.Marker({
         position: myLatlng,
         title: myTittle
     });

           var mapOptions = {
             center: myLatlng,
             zoom: 16,
             mapTypeId: google.maps.MapTypeId.ROADMAP
           };
           var map = new google.maps.Map(document.getElementById("map-canvas"),
               mapOptions);
     marker.setMap(map);    
      }
         google.maps.event.addDomListener(window, 'load', initialize);

  </script>

答案 4 :(得分:0)

我正在寻找如何映射当前位置和目的地位置。我这样做是为了实现这一点:

注意*替换: YOUR_DESTINATION_ADDRESS ,地址格式如下:“147 + Wyndham + Street + N +,+ Suite + 206,+ Guelph,+ On,+ N1H + 4E9”

<div id="Map" style="width: 100%; height: 600px;"> </div>
<script type="text/javascript">
$(function(){
    var zoom = 11;
    function success(position) {

        var LAT = position.coords.latitude;
        var LONG = position.coords.longitude;
        $('#Map').append('<p>Current Latitude: ' + LAT + '<br/>Current Logitude: ' + LONG +'<br/><br/>Note* This may not display your exact location, but just your city. Works better on mobile devices</p>');


        var map  = '<iframe style="width: 100%; height: 600px;" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.ca/maps?source=s_d&saddr=' + LAT +',+' + LONG +'&daddr=**YOUR_DESTINATION_ADDRESS**&ie=UTF8&t=hz=' + zoom + '&output=embed"></iframe>';

        $('#Map').append(map);

    }

    function error(){
        /*Error Code*/
    }

    function MakeMap(){
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(success, error);
        } 
    }

    MakeMap();
});
</script>