谷歌地图 - 当前位置

发布于 2024-08-26 02:35:01 字数 46 浏览 4 评论 0原文

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

非常感谢。 问候

I would like to get the address string of the current user location. Is that possible?

Many thanks.
Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

翻身的咸鱼 2024-09-02 02:35:01

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

google.loader.ClientLocation

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

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

the user's location is available in google.loader.ClientLocation (this will contain a guess based on the client IP if the IP is known)

google.loader.ClientLocation

When an application makes use of the
AJAX API loader, the loader attempts
to geo locate the client based on its
IP address. If this process succeeds,
the client's location, scoped to the
metro level, is made available in the
google.loader.ClientLocation property.
If the process fails to find a match,
this property is set to null.

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

醉生梦死 2024-09-02 02:35:01

如果您想获取街道地址

包括 JSMAP Api 密钥,将向导中的所有内容放在一起

我的代码如下所示:(还使用一点 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 阅读此链接也可能有所帮助:

获取纬度/经度对的街道地址

if you are trying to get The Street Address!

include both JS and MAP Api Key, keep all together from the wizard

my code look like this: (use also a bit of 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 read this link may help too:

Get street address at lat/long pair

眉黛浅 2024-09-02 02:35:01

如果您需要比 IP 地理定位更精确的东西,您可以使用 W3C 地理定位 API< /a> 在受支持的浏览器(尤其是 Firefox 3.5 和 Mobile Safari)中请求用户的纬度和经度,然后使用 Google 的客户端反向地理编码服务,用于猜测用户的大致街道地址。 (我的测试页面中包含这两个步骤的代码示例。)

如果您选择此路线,请务必预先向您的用户解释为什么您要请求他们的位置以及您如何使用他们的信息(例如,在他们实际提交表单之前您不会存储这些信息),因为从技术上来说这是 API 所要求的,并且因为某些原因用户会因为你能轻易猜到他们的街道地址而感到害怕。

If you need something more precise than IP geolocation, you could use the W3C Geolocation API in supported browsers (notably, Firefox 3.5 and Mobile Safari) to request the user's latitude and longitude and then use Google's client-side reverse geocoding service to guess the user's approximate street address. (Code samples for both of those steps included on my test page.)

If you go this route, be sure to explain to your users up front why you're requesting their location and how you're using their information (e.g. that you're not storing it until they actually submit a form) because it's technically required by the API and because some users will be creeped out by how easily you can guess their street address.

月寒剑心 2024-09-02 02:35:01

是的,可以使用 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>

Yes it is possible achieve using html 5 and Google API 3, in the given code you have to change your APIKey and map-canvas is a element in the html body

  <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>
只是在用心讲痛 2024-09-02 02:35:01

我一直在寻找如何绘制当前位置和目的地位置的地图。为了实现这一点,我所做的是:

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

I was looking to find how to map the current location and a destination location. To achieve this here is what I did:

Note* replace: YOUR_DESTINATION_ADDRESS with an address in a format like: "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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文