【问题标题】:Android Web App can't pick up GPSAndroid Web App 无法获取 GPS
【发布时间】:2011-03-02 11:15:36
【问题描述】:

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

当页面从 web 应用程序运行时,它会到达下面 js 中的“此处”警报,并且 当页面显示时,地图显示为 “错误:地理位置服务失败。”

有没有人编写过可以获取地理位置的本地网络应用程序? java web 应用程序或权限中必须有其他需要的东西。有没有人这样做过..

这是我尝试使用 google 示例的 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);
}

任何想法.. 谢谢麦克

【问题讨论】:

  • 你想做什么?我以前从未听说过原生网络应用程序。我假设您的意思是您制作了一个仅显示来自您的网络应用程序的页面的 Android 应用程序?
  • 是的,你是对的一个 android 应用程序,它显示一个带有地理位置的 web apge..

标签: javascript android gps webview emulation


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多