Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get geolocation working on appcelerator's titanium mobile application with android webview
    text
    copied!<p>I'm just playing with Appcelerator's Titanium platform for developing mobile apps.</p> <p>My test application just opens a webview pointing to an online web page. This page <a href="http://dev.w3.org/geo/api/spec-source.html" rel="nofollow">uses the W3C Geolocation API</a> to get user's location.</p> <p>This are my tiapp.xml specific android permissions:</p> <pre><code>&lt;android xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;manifest&gt; &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt; &lt;uses-permission android:name="android.permission.INTERNET"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt; &lt;uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/&gt; &lt;/manifest&gt; &lt;/android&gt; </code></pre> <p>This is my javascript code for getting coords:</p> <pre><code>if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position){ $("#results").append('Longitude: ' + position.coords.longitude + '&lt;br/&gt;'); $("#results").append('Latitude: ' + position.coords.latitude + '&lt;br/&gt;'); }, function(error){ $("#results").append('An error ocurred ' + error.message); }); } else { $("#results").append('Geolocation not supported'); } </code></pre> <p>It seems that navigator.geolocation and navigator.geolocation.getCurrentPosition are defined but delegate's are not executed anyway.</p> <p>The question is: how to get this working? :-)</p> <p>Thanks in advance.</p> <p><strong>Update:</strong> I found that the problem seems that Android 2.x webview has it's own implementation of navigator.geolocation. According to <a href="https://github.com/phonegap/phonegap-android/commit/5255f632377c36beb0b4d2620940f33b6d02b2c7" rel="nofollow">this commit</a> on phonegap's source code.</p> <p><strong>Update 2:</strong> I wrote a very small full-native android application that opens a webclient to same webpage and works fine:</p> <pre><code>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()); } } </code></pre> <p><strong>Update 3</strong>: I found this source from appcelerator's github site: <a href="https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java" rel="nofollow">https://github.com/appcelerator/titanium_mobile/blob/master/android/modules/ui/src/ti/modules/titanium/ui/widget/webview/TiWebChromeClient.java</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload