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

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

问题描述



这是我第一次使用这个通用汽车API,并且有很多东西我都不知道't懂。



这是我的代码,我想要

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

函数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({
位置:latLng,
标题:'Ambarrukmo Plaza Yogyakarta',
map:map,
可拖动:true
});
}

问题是,我想自动改变-7.7801502和110.3846387的值用户的当前位置。我可以这样做吗?



感谢您的帮助和解释。

另一个问题:
- >如果我要改变基于嵌入GPS的设备的价值,该怎么办?

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

另外一种方法是使用 HTML5地理位置API
$ b

  function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert(此浏览器不支持地理定位。);
}
}
function showPosition(position){
var lat = position.coords.latitude;
var lng = position.coords.longitude;
map.setCenter(new google.maps.LatLng(lat,lng));
}


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

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

Here is my code, and i want

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
  });
}

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.

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

解决方案

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.

An other way is to use the 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));
}

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

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