Question

I am an Android Developer and don't have any knowledge about JScript/Jquery/Phonegap etc. I found an example and I replace its web page with mine. In my web page I have an java script as follow:

 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">    </script>
<script type="text/javascript">var map;function initialize()
{
var mapDiv = document.getElementById('map-canvas');


map = new google.maps.Map(mapDiv, {center: new google.maps.LatLng(9.0000,-79.5000),zoom: 13,mapTypeId: google.maps.MapTypeId.SATELLITE});
google.maps.event.addListenerOnce(map, 'tilesloaded', addMarkers);}function addMarkers()
{
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
var latLng = new google.maps.LatLng(9.0000,-79.5000);
var marker = new google.maps.Marker({position: latLng,map: map});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

I want to change Lat-Long value dynamically Using my android code. Please some one guide me to do this.

These is the android code to load html page on web view.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.setWebViewClient(new NewsClient());
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.loadUrl("file:///android_asset/www/map.html");
}

private class NewsClient extends WebViewClient {

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        System.out.println("URL: " + url);
        view.loadUrl("javascript:changeLocation('" + url + "')");
        return true;
    }
}
Was it helpful?

Solution

I have an example android application in which I use a test plugin as a gateway to send values from native code to Javascript plugin and update the DOM values.

android-cordova-plugin

If you are familiar with phonegap plugins then it would be easier to understand the code. I simply use following method in my Activity class to update the html side. Here appcomm.updateValues() is my gateway plugin method which received the values from native code without initiating any call from Javascript.

public void sendValue(String value1, String value2) {
        JSONObject data = new JSONObject();
        try {
            data.put("value1", value1);
            data.put("value2", value2);
        } catch (JSONException e) {
            Log.e("CommTest", e.getMessage());
        }
        String js = String.format("window.plugins.appcomm.updateValues('%s');",
                data.toString());
        this.sendJavascript(js);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top