0

I'm building an android application that will integrate map from google maps api 3. I've read this tutorial and can't understand what this block of codes does.

private void setupWebView(){
  final String centerURL = "javascript:centerAt(" +
  mostRecentLocation.getLatitude() + "," +
  mostRecentLocation.getLongitude()+ ")";
  webView = (WebView) findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  //Wait for the page to load then send the location information
  webView.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url){
      webView.loadUrl(centerURL);
    }
  });
  webView.loadUrl(MAP_URL);
}

Can somebody explain to me thoroughly what this codes do. Thanks!

1 Answer 1

3

Late, but maybe it will help someone else.

I am not quite sure since I am a beginner, but my guess is you are calling a JavaScript function called "centerAt", take a look to this tutorial https://developers.google.com/maps/articles/android_v3, you will see the JavaScript bridge definition with that function "centerAt"

<script type="text/javascript" 
  src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
  var map;
  function initialize() {
    var latitude = 0;
    var longitude = 0;
    if (window.android){
      latitude = window.android.getLatitude();
      longitude = window.android.getLongitude();
    }
    var myLatlng = new google.maps.LatLng(latitude,longitude);
    var myOptions = {
      zoom: 8,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);
  }
  function centerAt(latitude, longitude){
    myLatlng = new google.maps.LatLng(latitude,longitude);
    map.panTo(myLatlng);
  }
  </script>

To sum up, what you are really doing is displaying a map in an Android app using a JavaScript bridge, so from the Android code you use the JavaScript functions defined on it, calling them trough an URL.

Again, I am beginner (frist day with Google Maps), probably I am wrong.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.