Built-in Google map cannot get current location in WebView

I followed this guide: http://android-er.blogspot.com/2013/03/embed-google-map-in-webview.html

I am trying to use a Google Map in a WebView, but cannot get my current location. I have included JavaScript in the WebView. What else do I need to enable?

Does anyone know why this could be? Doesn't that tell me to use my current location?

Please note that I am not interested in using MapView as an alternative. Am I trying to figure out what I need to install in WebView, or possibly in device location services?

+6
source share
4 answers

You must allow the web viewer to access your location by overriding the onGeolocationPermissionsShowPrompt method as follows:

 webView.setWebChromeClient(new WebChromeClient(){ @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } }); 
+2
source

You can try GreenDroid with Google Maps.

Note: https://github.com/cyrilmottier/GreenDroid

+1
source

In API 5.x and below you will need

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

in AndroidManifest.xml .

But to allow permissions for geolocation on API 6.0+, you must request permission at run time .

Request a placement permit

To do this, use

 private String mGeolocationOrigin; private GeolocationPermissions.Callback mGeolocationCallback; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // other setup myWebView.setWebChromeClient(new MyWebChromeClient()); } private WebChromeClient mWebChromeClient = new WebChromeClient() { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { // Geolocation permissions coming from this app Manifest will only be valid for devices with API_VERSION < 23. // On API 23 and above, we must check for permission, and possibly ask for it. final String permission = Manifest.permission.ACCESS_FINE_LOCATION; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || ContextCompat.checkSelfPermission(MainActivity.this, permission) == PackageManager.PERMISSION_GRANTED) { // we're on SDK < 23 OR user has already granted permission callback.invoke(origin, true, false); } else { if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) { // user has denied this permission before and selected [/] DON'T ASK ME AGAIN // TODO Best Practice: show an AlertDialog explaining why the user could allow this permission, then ask again } else { // ask the user for permissions ActivityCompat.requestPermissions(MainActivity.this, new String[] {permission}, RP_ACCESS_LOCATION); mGeolocationOrigin = origin; mGeolocationCallback = callback; } } } } 

and get the result:

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case RP_ACCESS_LOCATION: boolean allow = false; if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // user has allowed these permissions allow = true; } if (mGeolocationCallback != null) { mGeolocationCallback.invoke(mGeolocationOrigin, allow, false); } break; } } 

in your business.

+1
source

You need to enable android.permission.ACCESS_FINE_LOCATION and android.permission.INTERNET ,

Create an instance of LocationManager and LocationListener instance

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new MyLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener); 

and add the onLocationChanged(Location loc) method, which inside it has loc generates longitude and latitude ( String long = loc.getLongitude(); String lat = loc.getLatitude(); )

and now use long, lat to generate your mapPath line and continue generating WebView

You can use this for reference: http://www.rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html

0
source

Source: https://habr.com/ru/post/944723/


All Articles