2012-01-18 4 views
9

사용자의 위치 (위도, 경도)를 가져와 Bing Maps API 또는 Google Maps API를 사용하여 사용자의 경도 & 경도를 검색 할 수 있습니다. 반면,Bing 또는 Google Maps API로

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

하지만 그 기능은, 그것은 단순히지도에 위치를 설정 데이터를 반환하지 않습니다 나는 나는 "나를 autolocate의"코드를 본 것 같아요 기능은지도 자체에 사용자를 표시하기 위해 사용되었다 일부 계산을 수행하려면 해당 위치가 필요합니다. 즉, 미리 정의 된 작업 공간 목록 중 현재 사용자 위치에 가장 가까운 위치를 계산하십시오. 그 외에도,이 API 중 어떤 것이 입력으로 제공되는 두 위치 (위도, 경도) 사이의 거리를 계산할 수 있습니까?

감사합니다, 파블

답변

11

위치가지도 API의 일부가 아닙니다 얻기. 대신 위치를 찾으려면 HTML5 GeoLocation API을 사용하십시오. 예제는 다음과 같습니다

위도/LNG 점 사이의 거리를 계산에 대한
navigator.geolocation.getCurrentPosition(locationHandler); 

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

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; 
+0

감사합니다. 후속 질문에 대해 살펴보십시오. http://stackoverflow.com/questions/8917904/html5-geolocation-api-no-callback-is-called-whatsoever? 감사! – dragonfly

+1

toRad가 정의되지 않은 사람의 경우이 답변을 확인하십시오. http://stackoverflow.com/a/5260472/879821 – kavain

0

HTML5 GeoLocation은 현재 위치의 위도와 경도를 가져옵니다. http://www.w3schools.com/html/html5_geolocation.asp

<!DOCTYPE html> 
<html> 
<body> 

<p id="demo">Click the button to get your coordinates:</p> 
<button onclick="getLocation()">Try It</button> 

<script> 
var x = document.getElementById("demo"); 

function getLocation() { 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition); 
    } else { 
     x.innerHTML = "Geolocation is not supported by this browser."; 
    } 
} 

function showPosition(position) { 
    x.innerHTML="Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
} 
</script> 

</body> 
</html> 
관련 문제