Java Story

[ SPRING ] GPS 수신 하기

WhiteDuck 2017. 4. 21. 18:08

GPS 수신




// 작성일자 : 2017-04-21



최근 크롬 버전에서 GPS가 geolocation API 로 수신되지 않는다. 


다음과 같은 경고메시지를 크롬 브라우저의 개발자 도구에서 확인할 수 있다.


getCurrentPosition() and watchPosition() no longer work on insecure origins. ... use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://goo.gl/rStTGz for more details.



요약하면 보안상의 이유로 HTTPS를 사용하라는 이야기이다.


HTTPS는 HTTP 와 SSL을 함께 사용하는 보안 프로토콜인데, 이를 적용하려면 여러가지 해야할 일이 많아진다.


해서 다음과 같은 해결방법을 찾았다.


참고 : http://jsfiddle.net/gogs/jwt9f1o3/


var apiGeolocationSuccess = function(position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};

var tryAPIGeolocation = function() {
jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function(err) {
alert("API Geolocation error! \n\n"+err);
});
};

var browserGeolocationSuccess = function(position) {
alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};

var browserGeolocationFail = function(error) {
switch (error.code) {
case error.TIMEOUT:
alert("Browser geolocation error !\n\nTimeout.");
break;
case error.PERMISSION_DENIED:
if(error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !\n\nPosition unavailable.");
break;
}
};

var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail,
{maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
}
};

tryGeolocation();


동작 확인 완료!

반응형