开发者

Android Web App can't pick up GPS

开发者 https://www.devze.com 2023-02-14 20:24 出处:网络
I have a web app that displays a web page in a web view. But the web page can\'t pick up the getcurrentposition, however if i display the web page in the android broswer it does , but not in the web a

I have a web app that displays a web page in a web view. But the web page can't pick up the getcurrentposition, however if i display the web page in the android broswer it does , but not in the web app?

When the page is run from a web app, it gets to the 'here' alert in the js below and when the page is displayed the map is displayed with "Error: The Geolocation service failed."

Has anyone written a native web app, that can pick up geolocation? There must be something else needed in the java web app or permissions. has anyone done this..

here is the html i'm trying to use the google example,

http://code.google.com/apis/maps/documentation/javascript/examples/map-geolocation.html here is the js

var initialLocation;
var siberia = new google.maps.LatLng(60, 105);
var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
var browserSupportFlag = new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();

function initialize() {
    var myOptions = {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    // Try W3C Geolocation method (Preferred)
    if (navigator.geolocation) {
        browserSupportFlag = true;
        alert('here navigator');
        navigator.geolocation.getCurrentPosition(function (position) {
            initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            contentString = "Location found using W3C standard";
            map.setCenter(initialLocation);
            infowindow.setContent(contentString);
            infowindow.setPosition(initialLocation);
            infowindow.open(map);
        }, function () {
            handleNoGeolocation(browserSupportFlag);
        });
    } else if (google.gears) {
        // Try Google Gears Geolocation
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function (position) {
            initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
            contentString = "Location found using Google Gears";
            map.setCenter(initialLocation);
            infowindow.setContent(contentString);
            infowindow.setPosition(initialLocation);
            infowindow.open(map);
        }, function () {
            handleNoGeolocation(browserSupportFlag);
        });
    } else {
        // Browser doesn't support Geolo开发者_如何学编程cation
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }
}

function handleNoGeolocation(errorFlag) {
    if (errorFlag == true) {
        initialLocation = newyork;
        contentString = "Error: The Geolocation service failed.";
    } else {
        initialLocation = siberia;
        contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
    }
    map.setCenter(initialLocation);
    infowindow.setContent(contentString);
    infowindow.setPosition(initialLocation);
    infowindow.open(map);
}

any ideas.. thanks mike


Yes did get it working. But i wrote my own navigator object in js, and bridged to the java via a jsinterface. Basically i got GPS working in a non web app first. Then wrote a java script interface in java. And then called the jsinterface in java from js to get geo. The good thing about my approach is it should work on 1.5 and up and it works on the emulator. Bad news is that it takes shyt load of coding. This approach will work but unfortunately in 2.3 the javascript interface is not very stable - other versions of android are ok.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号