google maps

Google Maps example

With this example we are going to demonstrate how to use Google maps on Android. Note that in order to use Google Maps, the Google APIs have to be present in your SDK. In case they are not already installed, you will have to manually install them. This is accomplished by using the Android SDK and AVD Manager.

So, in order to use Google maps, the following steps will help you:

  • Create a class that extends the com.google.android.maps.MapActivity class (here called GMapsActivity)
  • Create a resource XML file that includes the com.google.android.maps.MapView
  • Use an instance of the com.google.android.maps.MapView class by taking reference of the one defined in the XML
  • Create a class that extends com.google.android.maps.ItemizedOverlay (here called CustomItemizedOverlay)
  • Implement the onTap of the ItemizedOverlay class
  • Launch the activity as usual via the XML launcher file

These are demonstrated in the code snippet(s) below:

package com.javacodegeeks.android.googlemaps;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GMapsActivity extends MapActivity {

    private MapView mapView;

    private static final int latitudeE6 = 37985339;
    private static final int longitudeE6 = 23716735;

    @Override
    public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  mapView = (MapView) findViewById(R.id.map_view);

  mapView.setBuiltInZoomControls(true);

  List<overlay> mapOverlays = mapView.getOverlays();

  Drawable drawable = this.getResources().getDrawable(R.drawable.icon);

  CustomItemizedOverlay itemizedOverlay = 

 new CustomItemizedOverlay(drawable, this);

  GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);

  OverlayItem overlayitem = 

 new OverlayItem(point, "Hello", "I'm in Athens, Greece!");

  itemizedOverlay.addOverlay(overlayitem);

  mapOverlays.add(itemizedOverlay);

  MapController mapController = mapView.getController();

  mapController.animateTo(point);

  mapController.setZoom(6);

    }

    @Override
    protected boolean isRouteDisplayed() {

  return false;
    }

}
package com.javacodegeeks.android.googlemaps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

   private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

   private Context context;

   public CustomItemizedOverlay(Drawable defaultMarker) {

  super(boundCenterBottom(defaultMarker));
   }

   public CustomItemizedOverlay(Drawable defaultMarker, Context context) {

  this(defaultMarker);

  this.context = context;
   }

   @Override
   protected OverlayItem createItem(int i) {

return mapOverlays.get(i);
   }

   @Override
   public int size() {

return mapOverlays.size();
   }

   @Override
   protected boolean onTap(int index) {

OverlayItem item = mapOverlays.get(index);

AlertDialog.Builder dialog = new AlertDialog.Builder(context);

dialog.setTitle(item.getTitle());

dialog.setMessage(item.getSnippet());

dialog.show();

return true;
   }

   public void addOverlay(OverlayItem overlay) {

mapOverlays.add(overlay);

 this.populate();
   }

}
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <com.google.android.maps.MapView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map_view"
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:clickable="true" 
  android:enabled="true" 
  android:apiKey="API-KEY-HERE" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.javacodegeeks.android.googlemaps"

android:versionCode="1"

android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

  <activity android:name=".GMapsActivity"

android:label="@string/app_name">

<intent-filter>

    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

  </activity>

<uses-library android:name="com.google.android.maps" />

    </application>

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

</manifest>

 
This was an Android Google Maps example.

Related Article:

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button