java - Unable to get GPS coordinates using Javascript webview in Android - Stack Overflow

I am very new to android, java and javascript coding.I am trying to get my current GPS co-ordinates and

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 .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=""
    package=".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=".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>

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 .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./apk/res/android"
    package=".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=".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>
Share Improve this question edited Sep 18, 2015 at 9:28 Valery Viktorovsky 6,7563 gold badges45 silver badges51 bronze badges asked Sep 6, 2013 at 18:38 Lawrence WongLawrence Wong 1,1494 gold badges24 silver badges40 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745413727a4626651.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信