簡體   English   中英

谷歌地圖 - 當前位置

[英]Google Map - Current location

我想獲取當前用戶位置的地址字符串。 那可能嗎?

非常感謝。 問候

用戶的位置在google.loader.ClientLocation中可用(如果知道IP,這將包含基於客戶端IP的猜測)

google.loader.ClientLocation

當應用程序使用AJAX API加載程序時,加載程序會嘗試根據其IP地址對客戶端進行地理定位。 如果此過程成功,則在google.loader.ClientLocation屬性中提供作用於地鐵級別的客戶端位置。 如果進程未能找到匹配項,則此屬性設置為null。

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

是的,有可能實現使用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>

如果您需要比IP地理位置更精確的東西,您可以在支持的瀏覽器(特別是Firefox 3.5和Mobile Safari)中使用W3C Geolocation API來請求用戶的經度和經度,然后使用Google的客戶端反向地理編碼服務來猜測用戶的近似街道地址。 我的測試頁面上包含的這兩個步驟的代碼示例。)

如果你走這條路,一定要事先向你的用戶解釋你為什么要求他們的位置以及你如何使用他們的信息(例如你在他們實際提交表格之前你沒有存儲它),因為這是技術上要求的通過API,因為一些用戶可以輕易地猜出他們的街道地址。

如果你想獲得街道地址

包括JSMAP Api Key, 從向導中保留所有內容

我的代碼看起來像這樣:(也使用一點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閱讀此鏈接也可能有所幫助:

獲取lat / long對的街道地址

我正在尋找如何映射當前位置和目的地位置。 我這樣做是為了實現這一點:

注意*替換: 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>

暫無
暫無

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

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