Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get GPS coordinates using Javascript webview in Android

I am very new to android, java and javascript coding.

I am trying to get my current GPS co-ordinates and display it on an Android device. I am using webview and the majority of the code is written in javascript.

However, my code did not work as a blank white screen with error code 2 is shown when my application is deploy on my android device.

I have been looking through lots of websites and I am still unable to resolve this problem.

Any help please?

The codes for the various files are located below.

map.js

navigator.geolocation.watchPosition(
    function(position){
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        document.getElementById("myPos").innerHTML = "lat:"+lat+"<br>long:"+lng;
    },
    function(error){
        document.getElementById("myPos").innerHTML = "Error Code:"+error.code;
    }
);
setTimeout("window.scrollTo(0,1)", 10);

main.css

body {
    width: 320px;
    height:480px;
    background-color : white;
}
#myMap {
    position : absolute;
    left : 0px;
    top: 0px;
    width: 320px;
    height: 480px;
}

MainActivity.java

package com.example.midnightmap;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView)findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
             public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                // callback.invoke(String origin, boolean allow, boolean remember);              
                callback.invoke(origin, true, false);
             }
            });
        //webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index.html");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.midnightmap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.midnightmap.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
like image 207
Lawrence Wong Avatar asked Sep 06 '13 18:09

Lawrence Wong


1 Answers

I realised I left out this in my Manifest.xml file My application worked after I add in

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
like image 139
Lawrence Wong Avatar answered Oct 20 '22 17:10

Lawrence Wong