2017-10-09 60 views
0

我正在將網站轉換爲Android應用程序。但GeoLocation無法在低於棒棒糖的版本上工作。那麼該如何解決?地理位置無法正常工作使用Webview的Android <5版本

我有一個簡單的WebView在我activity_main.xml中

我MainActivity.java代碼:

public class MainActivity extends ActionBarActivity { 

private static final String TAG = MainActivity.class.getSimpleName(); 
private WebView myWebView; 
private WebSettings webSettings; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myWebView = (WebView) findViewById(R.id.webView); 
    webSettings = myWebView.getSettings(); 

    myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    myWebView.getSettings().setBuiltInZoomControls(true); 
    myWebView.setWebViewClient(new GeoWebViewClient()); 
    myWebView.setWebViewClient(new Client()); 
    myWebView.getSettings().setJavaScriptEnabled(true); 
    myWebView.getSettings().setGeolocationEnabled(true); 
    myWebView.setWebChromeClient(new GeoWebChromeClient()); 
    webSettings.setGeolocationEnabled(true); 
    if (Build.VERSION.SDK_INT >= 19) { 
     myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
    } else if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 19) { 
     myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
    } 

    myWebView.loadUrl("http://webrivers.co.in/green_medic/lock_screen.html"); //change with your website 
    } 

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    // Check if the key event was the Back button and if there's history 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { 
     myWebView.goBack(); 
     return true; 
    } 
    // If it wasn't the Back key or there's no web page history, bubble up to the default 
    // system behavior (probably exit the activity) 
    return super.onKeyDown(keyCode, event); 
    } 

    public class GeoWebViewClient extends WebViewClient { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // When user clicks a hyperlink, load in the existing WebView 
     view.loadUrl(url); 
     return true; 
    } 
    } 

    public class GeoWebChromeClient extends WebChromeClient { 
    @Override 
    public void onGeolocationPermissionsShowPrompt(String origin, 

    GeolocationPermissions.Callback callback) { 
     // Always grant permission since the app itself requires location 
     // permission and the user has therefore already granted it 
     callback.invoke(origin, true, false); 
    } 
    } 

    public class Client extends WebViewClient { 
    ProgressDialog progressDialog; 

    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // If url contains mailto link then open Mail Intent 
     if (url.contains("mailto:")) { 
      // Could be cleverer and use a regex 
      //Open links in new browser 
      view.getContext().startActivity(
        new Intent(Intent.ACTION_VIEW, Uri.parse(url))); 
      // Here we can open new activity 
      return true; 
     } else { 
      // Stay within this webview and load url 
      view.loadUrl(url); 
      return true; 
     } 
    } 

    //Show loader on url load 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     // Then show progress Dialog 
     // in standard case YourActivity.this 
     if (progressDialog == null) { 
      progressDialog = new ProgressDialog(MainActivity.this); 
      progressDialog.setMessage("Loading..."); 
      progressDialog.show(); 
     } 
    } 

    // Called when all page resources loaded 
    public void onPageFinished(WebView view, String url) { 
     try { 
      // Close progressDialog 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
       progressDialog = null; 
      } 
     } catch (Exception exception) { 
      exception.printStackTrace(); 
     } 
    } 
    } 
    } 

以下版本中的地理位置做工精細的Android 5,但不能得到地理位置自動,一旦加載谷歌地圖,然後工作地理位置請幫助我。

+0

幫我嗎? –

回答

0

請或許檢查您的網址您的網址問題, 我檢查你的代碼不同的URL它的正常工作下面棒棒糖設備

+0

Hi @ Sabbir Ahmed –

+0

我曾經這個鏈接https://www.google.co.in/maps/@11.0168445,76.9558321,8z?hl=en轉換 –

+0

谷歌地圖說Google Map無法確定你的確切位置。 , –

相關問題