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
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
» | cordova 플러그인을 이용하여 GPS 활성화 | 황제낙엽 | 2017.01.14 | 672 |
14 |
모바일 크롬에서 단축 아이콘 생성 - WebApp 만들기
![]() | 황제낙엽 | 2016.12.03 | 228 |
13 | [JS] navigator geolocation 현재 접속 위치 가져오기 | 황제낙엽 | 2016.12.03 | 232 |
12 |
Geolocation API를 이용해 위치 정보를 알아내기
![]() | 황제낙엽 | 2016.12.03 | 256 |
11 |
[jQuery Mobile] 테마 (Theme)
![]() | 황제낙엽 | 2012.11.15 | 74 |
10 | [jQuery Mobile] 설정 | 황제낙엽 | 2012.11.15 | 84 |
9 | [jQuery Mobile] List view | 황제낙엽 | 2012.11.15 | 281 |
8 | [jQuery Mobile] Form element | 황제낙엽 | 2012.11.15 | 82 |
7 | [jQuery Mobile] Content formatting | 황제낙엽 | 2012.11.15 | 109 |
6 | [jQuery Mobile] Button | 황제낙엽 | 2012.11.15 | 77 |
5 | [jQuery Mobile] Toolbar | 황제낙엽 | 2012.11.15 | 75 |
4 | [jQuery Mobile] page & dialog | 황제낙엽 | 2012.11.15 | 81 |
3 | [jQuery Mobile] 컴포넌트란? | 황제낙엽 | 2012.11.15 | 79 |
2 |
[jQuery mobile] 기본 사용법
![]() | 황제낙엽 | 2012.11.15 | 115 |
1 | [jQuery mobile] 소개 | 황제낙엽 | 2012.11.15 | 131 |