如何使用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
最满意答案
您需要在对象上使用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
相关问答
更多-
QTcpSocket听(QTcpSocket listen)[2022-08-23]
要监听端口,你必须使用QTcpServer,检查套接字示例。 for listening on a port you have to use QTcpServer, checkout the socket examples. -
使用QTcpSocket时出错(Error while using QTcpSocket)[2023-04-28]
这是我用来在原始项目中打开MJPEG Streaming Server的代码。 也许它可以帮助你找到你的问题。 void MjpegStreamingEngine::StartServer(){ m_TcpHttpServer = new QTcpServer(); m_TcpHttpServer->connect(m_TcpHttpServer, SIGNAL(newConnection()), ... -
QTcpSocket和删除(QTcpSocket and deletion)[2022-09-11]
您需要在对象上使用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 ... -
QTcpSocket问题(QTcpSocket problem)[2022-04-23]
我有同样的问题,我找到了原因和解决方案。 connectToHost可能不会直接在主窗口的contstructor中调用。 为什么? 原因是,此时主消息循环尚未运行。 永远不会调用内部QAbstractSocketPrivate::fetchConnectionParameters() ,Qt套接字超时定时器认为永远不会建立连接。 解决方案是将其称为“延迟”,如同 QMetaObject::invokeMethod(this, "OnDelayedConnect", Qt::QueuedConnection ... -
QTcpSocket未连接(QTcpSocket Not Connecting)[2022-10-22]
实际上,您缺少一个基本的东西:能够连接到套接字,您需要一个正在侦听该地址和端口的进程。 因此,您实际上可以使用同一软件的两个实例建立连接,但是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 ...
-
读一个QTcpSocket(Reading a QTcpSocket)[2023-09-08]
最后,问题是试图读取所有的时间,而不是在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 ...
-
QTcpSocket重新连接方式(QTcpSocket reconnection way)[2022-11-26]
编程客户端和服务器项目时遇到了同样的问题。 我使用了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 ... -
QTcpSocket的生命周期(Life time of QTcpSocket)[2022-06-25]
TCP不会通知您断开连接,除非远程对等方明确发送断开请求(通过使用close()或shutdown()方法)或您尝试写入断开连接的套接字(在这种情况下,您会收到断开的管道信号) 解决此问题的经典方法是实现心跳消息传递系统,在一定程度的心跳不活动后,您关闭套接字,断定远程对等体突然死亡或存在网络问题。 TCP will not notify you of disconnections unless the remote peer explicitly sends disconnect request (by ...