sitelink1 | http://blog.naver.com/yellowin75/220847298821 |
---|---|
sitelink2 | |
sitelink3 |
구글맵 API(Google Maps API) 사용시 주의사항 - navigator.geolocation.getCurrentPosition
javascript로 navigator.geolocation을 사용하여 위치정보를 사용할때는 반드시... GPS(위치사용기능)가 활성화된 상태에서 "navigator.geolocation" 의 geolocation API를 사용해야한다.
웹페이지상에서 GPS가 비활성화된 상태에서 "navigator.geolocation.getCurrentPosition" 를 호출한뒤엔 GPS를 활성화해도 navigator.geolocation.getCurrentPosition API사용이 안된다.
웹페이지를 새로 고침해야하는데~
특히 하이브리드앱에서는 웹페이지와는 다르게... 네이티브API와 연동하여 GPS기능을 활성화할 수 있는 설정화면을 띄울 수 있는데~
이 설정화면에서 활성화한 이후에 geolocation API를 사용하도록 로직을 구성하면 된다.
아래는 cordova 플러그인을 사용한 방법이다.
아래 2개의 플러그인을 설치한후.
cordova plugin add cordova.plugins.diagnostic --save
cordova plugin add cordova-plugin-request-location-accuracy --save
아래 javascript 소스를 사용한다.
function checkAvailability(){
cordova.plugins.diagnostic.isGpsLocationAvailable(function(available){
console.log("GPS location is " + (available ? "available" : "not available"));
if(!available){
checkAuthorization();
}else{
console.log("GPS location is ready to use");
}
}, function(error){
console.error("The following error occurred: "+error);
});
}
function checkAuthorization(){
cordova.plugins.diagnostic.isLocationAuthorized(function(authorized){
console.log("Location is " + (authorized ? "authorized" : "unauthorized"));
if(authorized){
checkDeviceSetting();
}else{
cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
switch(status){
case cordova.plugins.diagnostic.permissionStatus.GRANTED:
console.log("Permission granted");
checkDeviceSetting();
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED:
console.log("Permission denied");
// User denied permission
break;
case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
console.log("Permission permanently denied");
// User denied permission permanently
break;
}
}, function(error){
console.error(error);
});
}
}, function(error){
console.error("The following error occurred: "+error);
});
}
function checkDeviceSetting(){
cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
console.log("GPS location setting is " + (enabled ? "enabled" : "disabled"));
if(!enabled){
cordova.plugins.locationAccuracy.request(function (success){
console.log("Successfully requested high accuracy location mode: "+success.message);
//여기가 GPS활성화를 했을경우(확인을 선택경우)
//이후 geolocation API 사용~~
}, function onRequestFailure(error){
//여기는 GPS활성화를 안했을경우(취소를 선택한경우)
//geolocation API 사용하면 안됨.
console.error("Accuracy request failed: error code="+error.code+"; error message="+error.message);
if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
if(confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
cordova.plugins.diagnostic.switchToLocationSettings();
}
}
}, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY);
}
}, function(error){
console.error("The following error occurred: "+error);
});
}
checkAvailability(); // start the check
참고 URL : https://developer.mozilla.org/ko/docs/WebAPI/Using_geolocation