使用 Bing 或 Google Maps API 获取用户的位置(纬度、经度) [英] Get user's location (latitude, longitude) with Bing or Google Maps API

查看:24
本文介绍了使用 Bing 或 Google Maps API 获取用户的位置(纬度、经度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检索纬度 &使用 Bing Maps API 或 Google Maps API 的用户的经度.我想我看到了一个代码片段,其中使用自动定位我"功能在地图本身上标记用户:

is there a way to retrieve a latitude & longitude of the user using Bing Maps API or Google Maps API. I think I saw an code snippet where a "autolocate me" feature was used to mark user on the Map itself:

var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);  
geoLocationProvider.getCurrentPosition(); 

但是该函数不返回任何数据,它只是在地图上设置位置,而我需要该位置来执行一些计算,即计算预定义的地点列表中哪个最接近当前用户位置.除此之外,是否有任何 API 可以计算作为输入提供的两个位置(纬度、经度)之间的距离?

But that function doesn't return any data, it simply set location on the map, whereas I need that location to perform some calculations, namely calculate which of predefined list of places is the closest to current user location. Apart from that, does any of this API's can calculate the distance between two locations (lat,lon) that are provided as an input?

谢谢,帕维尔

推荐答案

获取您的位置不是 Map API 的一部分.相反,请使用 HTML5 GeoLocation API 来获取您的位置.一个例子是:

Getting your location isn't part of the Map API. Instead, use the HTML5 GeoLocation API to get your location. An example would be:

 navigator.geolocation.getCurrentPosition(locationHandler);

 function locationHandler(position)
 {
   var lat = position.coords.latitude;
   var lng = position.coords.longitude;
 }

要计算 lat/lng 点之间的距离,请查看 http://www.movable-type.co.uk/scripts/latlong.html.

For calculating distance between lat/lng points, have a look at http://www.movable-type.co.uk/scripts/latlong.html.

// Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2011                   - www.movable-type.co.uk/scripts/latlong.html 
// where R is earth’s radius (mean radius = 6,371km);
// note that angles need to be in radians to pass to trig functions!
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;

这篇关于使用 Bing 或 Google Maps API 获取用户的位置(纬度、经度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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