웹 브라우저에서 GPS 위치 가져오기
저는 모바일 기반의 웹사이트를 개발하고 있습니다. 구글 지도를 통합했습니다. 구글 지도의 'From' 필드를 동적으로 채워야 합니다.
웹 브라우저에서 GPS 위치를 가져와 Google Map의 'From' 필드에 동적으로 채울 수 있습니까?
Geolocation API를 사용하면 다음 코드를 사용하는 것처럼 간단합니다.
navigator.geolocation.getCurrentPosition(function(location) {
console.log(location.coords.latitude);
console.log(location.coords.longitude);
console.log(location.coords.accuracy);
});
Google의 클라이언트 위치 API를 사용할 수도 있습니다.
이 문제는 모바일 브라우저의 GPS 위치를 감지할 수 있습니까?에서 논의한 바 있습니다.모바일 브라우저에서 위치 데이터를 가져옵니다.거기서 더 많은 정보를 찾을 수 있습니다.
토론 참조 브라우저에서 GPS 좌표를 얻는 방법
여기 모바일 장치에서 작동하는 예제 응용프로그램 Where Am I?에 대한 링크가 있습니다.안드로이드에서 테스트를 해봤는데 GPS 모듈을 시작해서 현재 좌표를 구했어요.실제 예제에서 출처를 분석할 수 있습니다.
모바일에서 빠르게 접속할 수 있도록 QR코드를 만들었습니다.
좀 더 구체적인 대답을 하기 위해서요.HTML5를 사용하면 지리 좌표를 얻을 수 있고 꽤 괜찮은 역할을 합니다.전반적으로 지리적 위치에 대한 브라우저 지원은 꽤 좋습니다. ie7과 ie8(그리고 오페라 미니)를 제외한 모든 주요 브라우저.IE9는 그 일을 하지만 가장 못합니다.caniuse.com 에서 확인하세요:
또한 사용자의 위치에 접근하려면 사용자의 승인이 필요하므로 이를 확인하고 전원이 꺼져 있을 경우를 대비해 적절한 지침을 제공해야 합니다.특히 아이폰의 경우 사파리의 사용 권한을 켜는 것이 조금 번거롭습니다.
이것을 사용하면 http://www.w3schools.com/html/html5_geolocation.asp 에서 모든 정보를 찾을 수 있습니다.
<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>
지오로케이션 API가 있지만 현재 브라우저 지원은 지상에서 다소 부족합니다.그런 것들에 관심을 가지는 대부분의 사이트는 GeoIP 데이터베이스를 사용합니다. (이러한 시스템의 부정확성에 대한 일반적인 조항이 포함되어 있습니다.파이어이글과 같이 사용자 협력이 필요한 타사 서비스도 살펴볼 수 있습니다.
관찰 가능
/*
function geo_success(position) {
do_something(position.coords.latitude, position.coords.longitude);
}
function geo_error() {
alert("Sorry, no position available.");
}
var geo_options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
*/
getLocation(): Observable<Position> {
return Observable.create((observer) => {
const watchID = navigator.geolocation.watchPosition((position: Position) => {
observer.next(position);
});
return () => {
navigator.geolocation.clearWatch(watchID);
};
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
최신 지방 화살표 기능을 사용해 보겠습니다.
navigator.geolocation.getCurrentPosition((loc) => {
console.log('The location in lat lon format is: [', loc.coords.latitude, ',', loc.coords.longitude, ']');
})
언급URL : https://stackoverflow.com/questions/2577305/get-gps-location-from-the-web-browser
'programing' 카테고리의 다른 글
MariaDB 데이터베이스 표시 권한 (0) | 2023.10.31 |
---|---|
Symfony와 WordPress가 함께 실행되는 nginx 구성 (0) | 2023.10.31 |
Twitter 부트스트랩의 둥근 모서리를 모두 제거하기 (0) | 2023.10.31 |
Eclipse에서 Python Extension 디버깅 (0) | 2023.10.31 |
CSS 네이티브 변수가 미디어 쿼리에서 작동하지 않음 (0) | 2023.10.31 |