首页 \ 问答 \ 如何使用android webview在appcelerator的钛移动应用程序上运行地理定位(How to get geolocation working on appcelerator's titanium mobile application with android webview)

如何使用android webview在appcelerator的钛移动应用程序上运行地理定位(How to get geolocation working on appcelerator's titanium mobile application with android webview)

我只是在玩Appcelerator的Titanium平台来开发移动应用程序。

我的测试应用程序只是打开一个指向在线网页的webview。 此页面使用W3C Geolocation API获取用户的位置。

这是我的tiapp.xml特定的android权限:

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    </manifest>
</android>

这是我获取coords的javascript代码:

if (navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(function(position){

        $("#results").append('Longitude: ' + position.coords.longitude + '<br/>');
        $("#results").append('Latitude: ' + position.coords.latitude + '<br/>');

    }, function(error){

        $("#results").append('An error ocurred ' + error.message);
    });

} else {

    $("#results").append('Geolocation not supported');
}

似乎定义了navigator.geolocation和navigator.geolocation.getCurrentPosition,但无论如何都没有执行委托。

问题是:如何让这个工作? :-)

提前致谢。

更新:我发现问题似乎是Android 2.x webview有自己的navigator.geolocation实现。 根据phonegap的源代码提交

更新2:我写了一个非常小的完全原生的Android应用程序,打开webclient到同一个网页,并正常工作:

package com.sourcerebels;

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

class MyClient extends WebChromeClient {

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
            Callback callback) {
        callback.invoke(origin, true, false);
    }
}

public class TestWebClient extends Activity {

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationDatabasePath("/data/data/testWebClient");
        webView.loadUrl("http://www.sourcerebels.com/index2.html");

        webView.setWebChromeClient(new MyClient());
    }
}

更新3 :我从appcelerator的github网站找到了这个来源: https//github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/ti/modules/titanium/ui/widget/webview/TiWebChromeClient。 java的


I'm just playing with Appcelerator's Titanium platform for developing mobile apps.

My test application just opens a webview pointing to an online web page. This page uses the W3C Geolocation API to get user's location.

This are my tiapp.xml specific android permissions:

<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    </manifest>
</android>

This is my javascript code for getting coords:

if (navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(function(position){

        $("#results").append('Longitude: ' + position.coords.longitude + '<br/>');
        $("#results").append('Latitude: ' + position.coords.latitude + '<br/>');

    }, function(error){

        $("#results").append('An error ocurred ' + error.message);
    });

} else {

    $("#results").append('Geolocation not supported');
}

It seems that navigator.geolocation and navigator.geolocation.getCurrentPosition are defined but delegate's are not executed anyway.

The question is: how to get this working? :-)

Thanks in advance.

Update: I found that the problem seems that Android 2.x webview has it's own implementation of navigator.geolocation. According to this commit on phonegap's source code.

Update 2: I wrote a very small full-native android application that opens a webclient to same webpage and works fine:

package com.sourcerebels;

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

class MyClient extends WebChromeClient {

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
            Callback callback) {
        callback.invoke(origin, true, false);
    }
}

public class TestWebClient extends Activity {

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationDatabasePath("/data/data/testWebClient");
        webView.loadUrl("http://www.sourcerebels.com/index2.html");

        webView.setWebChromeClient(new MyClient());
    }
}

Update 3: I found this source from appcelerator's github site: https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java


原文:https://stackoverflow.com/questions/5933699
更新时间:2023-03-28 14:03

最满意答案

您需要在对象上使用deleteLater。 在删除QTCPSocket后,传入的消息可能会进入。 它记录在助手中。 你可以在这里找到一个例子:qthelp://com.trolltech.qt.472/qdoc/qt4-network.html

中号


You need to use deleteLater on the Object. Incoming messages may come in after you delete the QTCPSocket. It is documented in Assistant. You can find an example here: qthelp://com.trolltech.qt.472/qdoc/qt4-network.html

M

相关问答

更多
  • 要监听端口,你必须使用QTcpServer,检查套接字示例。 for listening on a port you have to use QTcpServer, checkout the socket examples.
  • 这是我用来在原始项目中打开MJPEG Streaming Server的代码。 也许它可以帮助你找到你的问题。 void MjpegStreamingEngine::StartServer(){ m_TcpHttpServer = new QTcpServer(); m_TcpHttpServer->connect(m_TcpHttpServer, SIGNAL(newConnection()), ...
  • 您需要在对象上使用deleteLater。 在删除QTCPSocket后,传入的消息可能会进入。 它记录在助手中。 你可以在这里找到一个例子:qthelp://com.trolltech.qt.472/qdoc/qt4-network.html 中号 You need to use deleteLater on the Object. Incoming messages may come in after you delete the QTCPSocket. It is documented in Assi ...
  • 我有同样的问题,我找到了原因和解决方案。 connectToHost可能不会直接在主窗口的contstructor中调用。 为什么? 原因是,此时主消息循环尚未运行。 永远不会调用内部QAbstractSocketPrivate::fetchConnectionParameters() ,Qt套接字超时定时器认为永远不会建立连接。 解决方案是将其称为“延迟”,如同 QMetaObject::invokeMethod(this, "OnDelayedConnect", Qt::QueuedConnection ...
  • 实际上,您缺少一个基本的东西:能够连接到套接字,您需要一个正在侦听该地址和端口的进程。 因此,您实际上可以使用同一软件的两个实例建立连接,但是2个实例中的一个必须充当服务器(并监听),第二个实例必须尝试进行连接。 到目前为止,您只实现了第二部分。 所以你应该添加如下内容: void Widget::on_listen_clicked() { if (!m_socket->listen()) { QMessageBox::information(this, tr("Tcp Server ...
  • 在罗马做到入乡随俗。 如果您的框架使用一种方法(例如Qt依赖于父 - 子关系),请在您的代码中使用此方法。 当然,不要忘记一般的良好内存管理实践:尽可能在堆栈上创建对象,使用共享指针等。 When in Rome, do as the Romans do. If your framework uses one method (for example Qt relies on parent-child relationship), use this method in your code. Of course ...
  • 最后,问题是试图读取所有的时间,而不是在read函数返回的第一个零之后等待新数据。 这样做就像一个魅力! Finally, the problem was trying to read all the time instead of waiting for new data after the first zero returned by the read function. Doing that works like a charm!
  • 如果能够阻止当前线程直到连接或超时, QAbstractSocket :: waitForConnected将为您提供您想要的内容。 如果没有,则使用信号QAbstractSocket :: stateChanged 。 例如在你的班级宣言中有 class C { public slots: void HandleStateChange(AbstractSocket::SocketState socketState ); ... }; 明确地定义它,然后在声明tcpClien ...
  • 编程客户端和服务器项目时遇到了同样的问题。 我使用了SIGNAL - > disconnected(),它在断开连接时由客户端立即发出。 connect(client,SIGNAL(disconnected()),this,SLOT(DisconnectMessage())); 请尝试使用它。 I had a same problem while programming client and server project. I used SIGNAL -> disconnected() which is e ...
  • TCP不会通知您断开连接,除非远程对等方明确发送断开请求(通过使用close()或shutdown()方法)或您尝试写入断开连接的套接字(在这种情况下,您会收到断开的管道信号) 解决此问题的经典方法是实现心跳消息传递系统,在一定程度的心跳不活动后,您关闭套接字,断定远程对等体突然死亡或存在网络问题。 TCP will not notify you of disconnections unless the remote peer explicitly sends disconnect request (by ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。