2010-02-15 37 views
12

我一直與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); 
} 

} 

} 

回答

22

我只是Android 2.1的試穿了Nexus One的代碼,並能正常工作。請記住,你需要必要的權限添加到您的清單:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
+0

感謝您的回覆和附加信息,羅馬。我很高興聽到這個問題已在2.1中得到解決。我很驚訝我發佈的代碼工作 - 我只是扔了所有的GeoPermissions的東西在那裏,以說明我想要做的事情,如果有人可以告訴我正確的方式來做到這一點。任何想法,如果有辦法讓它在2.0.1上的WebView中工作?目前我正在解決navigator.geolocation問題,因爲它似乎無法在2.0.1 WebView中使用。再次感謝! – ajh158 2010-04-01 14:25:55

3

我們(PhoneGap團隊)最近爲此發佈了一個修復程序。基本上,問題在於,從Android 2.0開始,原生WebView現在擁有自己的navigator.geolocation實現,因此在JavaScript中沒有正確設置此函數的PhoneGap橋接。

此後,特定提交創造了一種解決方法:我們「代理」本地navigator.geolocation對象我們自己的這個對象的定義。該對象與PhoneGap框架很好地協作。

對於此修訂,請參閱以下承諾:http://github.com/phonegap/phonegap-android/commit/5255f632377c36beb0b4d2620940f33b6d02b2c7

+0

感謝FIL少校 - 我知道,除了原生對象的打破的PhoneGap。我最終做了類似於您的解決方法的工作,但我希望我們能夠找到一種方法來在WebView中使用本地地理位置對象。我的代碼是爲了證明這不能從WebView(phonegap或no)中工作。 – ajh158 2010-03-01 14:26:37

+0

fil,我是新來的電話差距,你的承諾將被納入下一個版本?什麼時候?我不想削減我自己的版本,但如果你給的指示,會很高興地從你的分支切除。 – 2011-02-22 19:01:18

+1

@fil,你的URL是404.請更新。 – 2013-12-18 18:16:58

相關問題