Skip to content

Google maps plugin

Joan edited this page Apr 6, 2014 · 6 revisions

Google maps plugin provides some utilities to plot a World object in to a the google maps view. To do that first we need to import the jar file in to our libs folder (jar & src).

Once the jar is in our libs folder we set up our Android project to use the google-play-services library.

Then we just need to write some little code in our activity:

// ...
private GoogleMap mMap;
private GoogleMapWorldPlugin mGoogleMapPlugin;
// ...

@Override
protected void onCreate(Bundle savedInstanceState) {
     // ...
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

     // We create the world...
     mWorld = new World(this);
     // As we want to use GoogleMaps, we are going to create the plugin and
     // attach it to the World
     mGoogleMapPlugin = new GoogleMapWorldPlugin(context);
     // Then we need to set the map in to the GoogleMapPlugin
     mGoogleMapPlugin.setGoogleMap(mMap);
     // Now that we have the plugin created let's add it in to our world
     mWorld.addPlugin(mGoogleMapPlugin);
     
     // Now we fill the world
     // ...
}

The GoogleMapWorldPlugin will take care of drawing all the GeoObjects in the GoogleMap object. So we also can add the a listener to the map to get notify when a Marker is clicked and then we can check which GeoObject is belongs to the clicked Marker:

@Override
protected void onCreate(Bundle savedInstanceState) {
     // ...
     mMap.setOnMarkerClickListener(this);
     // ...
}

@Override
public boolean onMarkerClick(Marker marker) {
     // To get the GeoObject that owns the marker we use the following
     // method:
     GeoObject geoObject = mGoogleMapPlugin.getGeoObjectOwner(marker);
     if (geoObject != null) {
          Toast.makeText(this, "Click on a marker owned by a GeoOject with the name: " + geoObject.getName(), Toast.LENGTH_SHORT).show();
     }
     return false;
}

Example code

You can find the source code of a complete example here.

Clone this wiki locally