How to get geolocation running on appcelerator titanium mobile app with Android web overview

I just play with the Appcelerator Titanium platform for developing mobile applications.

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

These are my special permissions for android tiapp.xml:

<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 to get the coordinates:

 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 delegation is not done anyway.

Question: how to make this work ?:-)

Thanks in advance.

Update: I found that the problem is that Android 2.x webview has its own implementation of navigator.geolocation. In accordance with this fix on the source code of the phone book.

Update 2:. I wrote a very small, full-featured Android application that opens a web client on the same web page and works great:

 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 the gsiteub appcelerator website: https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java

+4
source share
3 answers

Finally, I solved this β€œproblem” by modifying TiWebChromeClient.java from the Titanium mobile SDK sources and creating a new titanium-ui.jar file.

Added this code, and now I can use geolocation in the Android web browser:

 import android.webkit.GeolocationPermissions.Callback; ... @Override public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) { callback.invoke(origin, true, false); } 
+3
source

The strongest titanium aspect is that it is the native code on every platform. Using a web survey, you avoid preemptive code.

http://wiki.appcelerator.org/display/guides/Using+Location+services

http://wiki.appcelerator.org/display/guides/HTML5+vs+Native+UI

In the "Use HTML" section, it states the following:

"HTML should be avoided as much as possible. Almost everything you need can be done using native code. The application will load faster and respond more quickly to user actions, resulting in a better user experience."

Following these standards, I would advise you to get the location, and then create or update a web view. As for why javascript is not working, I have no answer because the full DOM loads when creating the web view?

+4
source

Our solution is as follows:

 import android.webkit.GeolocationPermissions; // enable navigator.geolocation mWebView.getSettings().setGeolocationEnabled(true); mWebView.getSettings().setGeolocationDatabasePath("/data/data/databases/"); // START Allow Geolocation prompt public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } 
+1
source

All Articles