Android Web App无法接收GPS

时间:2011-03-02 11:15:37

标签: javascript android gps webview emulation

我有一个在Web视图中显示网页的Web应用程序。但是网页无法获取getcurrentposition,但是如果我在android broswer中显示网页,但不是在网络应用程序中显示?

当页面从Web应用程序运行时,它会转到下面js中的“here”警报  当显示页面时,将显示地图  “错误:地理位置服务失败。”

有没有人写过本地网页应用,可以选择地理位置?在Java Web应用程序或权限中必须有其他东西。有谁做过这个..

这是html我正在尝试使用谷歌示例

http://code.google.com/apis/maps/documentation/javascript/examples/map-geolocation.html  这是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 Geolocation
        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);
}

任何想法.. 谢谢迈克

1 个答案:

答案 0 :(得分:0)

是的,确实让它运转了。但我在js中编写了自己的导航器对象,并通过jsinterface桥接到java。基本上我首先在非网络应用程序中使用GPS。然后在java中编写了一个java脚本接口。然后从js中调用java中的jsinterface来获取geo。我的方法的好处是它应该在1.5及更高版本上工作,它可以在模拟器上运行。坏消息是它需要大量的编码。这种方法可行但不幸的是在2.3中javascript界面​​不是很稳定 - 其他版本的android都可以。