0

これをできる限り明確にしようと思います。私は通常、明確な質問をするのが得意ではないので、これを読んで提案を投稿してくれてありがとう。

ユーザーの位置を必要とする単純な Android アプリケーションを作成しています。ユーザーを追跡するために、HTML navigator.geolocation.getCurrentPosition 機能と一緒に webview を使用しています。

私が抱えている問題は、HTML ファイルで収集された座標を Android/Java アプリケーションに取得することです。私は webview 内で javascriptInterface を使用して、2 つの間で通信しています。

これが私のJavaコードでの私のwebviewの宣言です。

//Create the web-view
final WebView 
wv = (WebView) findViewById(R.id.webview);  
wv.getSettings().setJavaScriptEnabled(true);
//Creates the interface "Android". This class can now be referenced in the HTML file.
wv.addJavascriptInterface(new WebAppInterface(this),"Android");
wv.setWebChromeClient(new WebChromeClient());

ここに WebAppInterface コードがあります

public class WebAppInterface extends Activity 
 {
  Context mContext;

  /** Instantiate the interface and set the context */
  WebAppInterface(Context c) {mContext = c;}

  //Used to display Java Toast messages for testing and debugging purposes 
  @JavascriptInterface
  public void showToast(String toast) {
      Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
 }

  @JavascriptInterface
  public void storeCoord(String myLat,String myLong) 
  {
    Log.d("Coordinate Log","HTML call to storeCoords()");

    //Log.d("Coord Lat", myLat);
    //Log.d("Coord Long", myLong);

  }
}

最後になりましたが、これが私の HTML コードです。

<!DOCTYPE html>
<html>
  <body>        
    <script type="text/javascript">

        function storePosition(position) 
        {
            Android.showToast("In storePosition()");                                        

            myLat = position.coords.latitude;
            myLong = position.coords.longitude;

            Android.showToast(myLat +" "+ myLong);  
                            Android.storeCoord(myLat,myLong);               
        }

        function fail(error){}  

        if (navigator.geolocation) 
        {
            navigator.geolocation.getCurrentPosition(storePosition,fail,{enableHighAccuracy:true,timeout:10000});
        }   
    </script>
</body>

現在、ユーザーの座標を収集するたびに、次のコード行を使用しています

wv.loadUrl("file:///android_asset/CurrentLocationCoordinates.html");

storePosition 関数が呼び出されるまで、これは正常に機能します。loadURL から storePosition 関数を具体的に呼び出していないという事実と関係があると考えたので、試してみました

wv.loadUrl("javascript:storePosition()");

これは機能します。しかし!! ... navigator.geolocation.getCurrentPosition から収集された位置を関数 storedPosition に送信する必要がないため、明らかに何も起こりません。私は解決策を求めて Web 上のあらゆる場所を検索しましたが、webview.LoadURL 関数がどのように機能するかを単に理解していないという結論に達しました。Android アプリケーションに保存されている座標を取得する必要があります。

あなたの時間とあなたの提案に再び感謝します.

4

0 に答える 0