Can not pass Latitude and Longitude from HTML file of Leaflet to Android Activity

43
September 03, 2019, at 3:20 PM

I write a code identify user's current location by using Leaflet Map. The map works well and it can be able to identify user's current location and displays it. However, when I try to pass the values of latitude and longitude of user's current location from html file to normal android activity, it just return null all the time. Please help me fix this problem. Thank you so much.

This is the function to identify user's current location on the map. The e.latlng works well and marker can display on user's current location with latitude and longitude shown up on marker.

function onLocationFound(e) {
            var radius = e.accuracy;
            map.setView(e.latlng, 17);
            L.marker(e.latlng).addTo(map)
            .bindPopup("You are in " + e.latlng).openPopup();
            L.circle(e.latlng, radius).addTo(map);
            Android.getCurrentLocation(e.latlng.lat, e.latlng.lng);
        }
        map.on('locationfound', onLocationFound);

However, when I try to send latitude and longitude to normal Android activity. The toast shows that "null:null"

My WebAppInterface

public class WebAppInterface {
    private Context context;
    WebAppInterface(Context context){
        this.context = context;
    }
    @JavascriptInterface
    public void getCurrentLocation(Double latitude, Double longitude){
        Toast.makeText(context, latitude + ":" + longitude, Toast.LENGTH_LONG).show();
    }
}

My webView settings

public void openStreetMap() {
        webView.setWebChromeClient(new WebChromeClient() {
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }
        });
        webSettings = webView.getSettings();
        webSettings.setGeolocationEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        webView.loadUrl("file:///android_asset/leaflet.html");
    }
READ ALSO
Default ripple animation on a button resumes when a fragment is reloaded

Default ripple animation on a button resumes when a fragment is reloaded

I use buttons to go between fragments in an activityThe fragment transition happens faster than the default ripple animation on the button

55
onBackPressed disabled method in adroid studio

onBackPressed disabled method in adroid studio

For webview back button in android studio"onBackPressed" methot not working

53
Suddenly after implementing firebase many import statements becomes unused and can't resolve many symbols

Suddenly after implementing firebase many import statements becomes unused and can't resolve many symbols

I'm new to Android, Was learning from youtube from following playlist

46
Is there an event to check when the recipient answered the call?

Is there an event to check when the recipient answered the call?

I want to catch when the recipient gets a callTelephonyManager

41