2017-10-09 65 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无法确定你的确切位置。 , –

相关问题