我一直與PhoneGap的,它是偉大的,但我已經運行到與Verizon的Droid的獲取位置W/2.0中的問題。 1(按預期在G1 w/1.6上工作)。使用navigator.geolocation.getCurrentPosition中的WebView在Android 2.0+(PhoneGap的相關)
地理位置API支持加入到Android 2.0(Eclair的)和它的作品在默認瀏覽器上,Verizon的Droid的(上2.0.1)。也就是說,如果我訪問了一個調用navigator.geolocation.getCurrentPosition(success_callback,error_callback)的網站,設備會在對話框中提示當前域名「想知道您的位置」,並顯示「共享位置」或「拒絕」選項。如果我選擇「共享位置」,success_callback最終會被調用位置數據。
如果我訪問同一個網站的網頁視圖,調用navigator.geolocation.getCurrentPosition不會產生JavaScript錯誤,但不顯示「分享你的位置」對話框,並沒有回調曾經被調用。在logcat中,我看到了什麼似乎是一個相關的錯誤:「02-15 10:37:00.413:ERROR/geolocationService(16871):捕獲安全異常註冊位置更新來自系統,這應該只發生在DumpRenderTree中。
在我看來,該Webview是無法註冊的位置更新,因爲它不具備所需的權限,而這又是不是提示輸入權限的用戶的結果。雖然有幾種方法和對象添加到Android 2.0中與GeoPermissions相關的Webkit包中,但我還沒有能夠使用它們中的任何一個來讓WebView顯示GeoPermission對話框。
以下是基於你好,從Android開發者指南的WebView的例子,但它增加了一些在2.0中添加的有關GeoPermissions調用和對象。 *更新了適當的網址(獲得the author - thanks Oliver的許可!)。
有沒有人能得到這個工作?任何反饋將是偉大的,謝謝!
package com.example.android.helloactivity;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.GeolocationPermissions.Callback;
public class HelloActivity extends Activity implements GeolocationPermissions.Callback{
WebView webview;
String geoWebsiteURL = "http://maxheapsize.com/static/html5geolocationdemo.html";
public HelloActivity() {
}
/**
* Called with the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_activity);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setGeolocationEnabled(true); //seems like if i set this, the webview should prompt when I call navigator.geolocation.getCurrentPosition
GeolocationPermissions geoPerm = new GeolocationPermissions(); //added in API Level 5 but no methods exposed until API level 7
GeoClient geo = new GeoClient();
webview.setWebChromeClient(geo);
String origin = ""; //how to get origin in correct format?
geo.onGeolocationPermissionsShowPrompt(origin, this); //obviously not how this is meant to be used but expected usage not documented
webview.loadUrl(geoWebsiteURL);
}
public void invoke(String origin, boolean allow, boolean remember) {
}
final class GeoClient extends WebChromeClient {
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
Callback callback) {
// TODO Auto-generated method stub
super.onGeolocationPermissionsShowPrompt(origin, callback);
callback.invoke(origin, true, false);
}
}
}
感謝您的回覆和附加信息,羅馬。我很高興聽到這個問題已在2.1中得到解決。我很驚訝我發佈的代碼工作 - 我只是扔了所有的GeoPermissions的東西在那裏,以說明我想要做的事情,如果有人可以告訴我正確的方式來做到這一點。任何想法,如果有辦法讓它在2.0.1上的WebView中工作?目前我正在解決navigator.geolocation問題,因爲它似乎無法在2.0.1 WebView中使用。再次感謝! – ajh158 2010-04-01 14:25:55