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

 

번호 제목 글쓴이 날짜 조회 수
38 Google Map에 현재 위치 주변의 음식점 표시하기 황제낙엽 2017.01.23 8460
37 TTS 이용하기 (구글, 네이버, KT) file 황제낙엽 2018.11.01 1001
36 피들러(Fiddler)를 이용하여 모바일 네트웍 감시 file 황제낙엽 2020.02.24 735
35 팬(Pan), 스와이프(Swipe), 스크롤(Scroll), 플릭(Flick) file 황제낙엽 2017.09.11 630
» cordova 플러그인을 이용하여 GPS 활성화 황제낙엽 2017.01.14 588
33 음성합성(이하 TTS)관련 사양조사 및 시장조사 file 황제낙엽 2018.11.01 419
32 [Mysql] 위도 경도를 이용해 특정 거리안에 위치(거리) 구하기 황제낙엽 2017.01.25 357
31 Google Maps Geolocation API를 이용한 현재위치 검색 file 황제낙엽 2017.01.14 343
30 위치정보(GeoLocation)에서 위도와 경도로 두 지점간 거리(distance) 구하는 오라클 Function 황제낙엽 2017.01.25 249
29 모바일웹 SMS 황제낙엽 2019.09.28 239
28 원격 데스크톱 환경을 위한 터치에 대한 제스쳐 목록 정의 황제낙엽 2018.06.12 239
27 [jQuery Mobile] List view 황제낙엽 2012.11.15 221
26 Geolocation API를 이용해 위치 정보를 알아내기 file 황제낙엽 2016.12.03 196
25 [JS] navigator geolocation 현재 접속 위치 가져오기 황제낙엽 2016.12.03 179
24 모바일 크롬에서 단축 아이콘 생성 - WebApp 만들기 file 황제낙엽 2016.12.03 165
23 PhoneGap, Accelerator Titanium, Appspresso 황제낙엽 2013.11.29 138
22 [모바일 브라우저] Android 기기 원격 디버깅 시작하기 (PC크롬에서 모바일크롬 디버깅) file 황제낙엽 2019.01.14 125
21 Galaxy Z Fold3 5G Specifications 황제낙엽 2022.03.07 101
20 [Android] / 안드로이드 / googlemap distance / 구글맵 두 위치 사이의 거리 구하기 황제낙엽 2017.01.23 97
19 안드로이드 모바일 브라우저 캐시 지우기 황제낙엽 2019.07.23 90