在谷歌地图上获取当前位置 [英] Get Current Location On Google Map

查看:104
本文介绍了在谷歌地图上获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想获取当前位置时遇到了一些麻烦.

I'm having some trouble when i want to get current location.

这是我第一次使用这个 GM API,还有很多我不明白的地方.

This is my first time using this GM API, and there are so much things i don't understand.

这是我的代码,我想要

var geocoder = new google.maps.Geocoder();

function initialize() {
  var latLng = new google.maps.LatLng(-7.7801502, 110.3846387);
  var map = new google.maps.Map(document.getElementById('mapCanvas'), {
    zoom: 15,
    center: latLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  var marker = new google.maps.Marker({
    position: latLng,
    title: 'Ambarrukmo Plaza Yogyakarta',
    map: map,
    draggable: true
  });
}

问题是,我想根据用户的当前位置自动更改 -7.7801502 和 110.3846387 值.我可以吗?

The problem is, i want to change -7.7801502 and 110.3846387 value automatically based on user's current position. Can i do that?

之前感谢您的帮助和解释.

Thanks before for your help and explanation.

另一个问题:-> 如果我要根据嵌入 GPS 的设备更改这些值怎么办?

Another question : -> What if i'm going to change those value based on a device embedded with GPS?

推荐答案

您无法使用 Google 地图获取当前用户的位置.但是,如果您使用 Google Loader 获取 Google 地图,则可以使用 google.loader.CurrentLocation 根据 IP 获取位置.

You can't get current user's location with Google Maps. However, if you get Google Maps with Google Loader, you can use the google.loader.CurrentLocation to get a location based on IP.

另一种方法是使用 HTML5 GeoLocation API.

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
  } else {
    alert("Geolocation is not supported by this browser.");
  }
}
function showPosition(position) {
  var lat = position.coords.latitude;
  var lng = position.coords.longitude;
  map.setCenter(new google.maps.LatLng(lat, lng));
}

这篇关于在谷歌地图上获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆