Search code examples
androidcordovageolocation

Error in cordova navigator.geolocation.getCurrentPosition(code : 2)


when I try to get my geographical position using phonegap simulator, it is successful. But when I installed the application in my real android(build with android studio), it is failed to get geographical position.(I got error code : 2). I couldn't solve this problem help me plz.......

- in simulation circumstance with phonegap

- in installed circumstance

here is my code(index.html)

    <!DOCTYPE html>
<head>
    <meta charset="utf-8" />
    <title>AppTest</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="jquery.mobile/jquery.mobile.min.css" />
    <link rel="stylesheet" href="index.css" />
    <script type="text/javascript" src="jquery.mobile/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.mobile/jquery.mobile.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBlBJFTru5Cp2Ow-HC_w-GiUXbdP_QT3Rk&v=3.exp&sensor=false"></script>
<!--    <script type="text/javascript" charset="utf-8" src="index.js"></script>-->
</head>
<body>
    <div class="out_area">
        <!-- API 실행 버튼 -->
        <div data-role="button" class="btn1">
            API RUN
        </div>
        <!-- 결과 표시 영역 -->
        <div id="cur_position"></div>
        <div id="map-canvas"></div>
    </div>
    <script type=text/javascript>
var success = function(pos) {
    // 화면에 위치 정보를 보여줌
    var text = "<div>Latitude: " + pos.coords.latitude + "<br/>" + "Longitude: " + pos.coords.longitude + "<br/>" + "Accuracy: " + pos.coords.accuracy + "m<br/>" + "</div>";
    $("#cur_position").html(text);

    // 구글 맵 API를 이용한 지도 표시
    var map;
    var mapOptions = {
        zoom : 8,
        center : new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude)
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
};

// 에러 시 실행
var fail = function(error) {
    $("#cur_position").html("Error getting geolocation: " + error.code);
};        
    document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    alert('sss');
    navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 300000, timeout:10000, enableHighAccuracy : true});
};
    </script>
</body>
</html>

config.xml:

  <?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="[email protected]" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-intent href="market:*" />
    <preference name="loglevel" value="DEBUG" />
</widget>

AndroidManifest.xml:

    <?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="io.cordova.hellocordova" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
        <uses-permission android:name="android.permission.ACCESS_GPS" />
        <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="25" />
</manifest>

Thanks in advance p.s I can't english well.So if you have trouble reading my contens.. I'm really sorry for that... thanks!


Solution

  • change sensor to true and try again

     <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBlBJFTru5Cp2Ow-HC_w-GiUXbdP_QT3Rk&v=3.exp&sensor=false"></script>
    

    to

    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBlBJFTru5Cp2Ow-HC_w-GiUXbdP_QT3Rk&v=3.exp&sensor=true"></script>
    

    Hope it helps

    if above one don't work then try below code,

    if (navigator.geolocation) {
        var location_timeout = setTimeout("geolocFail()", 10000);
    
        navigator.geolocation.getCurrentPosition(function(position) {
            clearTimeout(location_timeout);
    
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
    
            // your stuff here
    
        }, function(error) {
            clearTimeout(location_timeout);
            geolocFail();
        });
    } else {
        // Fallback for no geolocation
        geolocFail();
    }