Today I’ll explain how to load the Google map on to your device. When creating a normal android application you are using the Activity class but here you have to use the MapActivity class. The following code segment shows the implementation of the MapActivity class. In addition to this class you have to write the xml file to create the interface.
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MarkOnMap extends MapActivity {
private MapView mapView;
private MapController mapController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
The xml file should include the following tag. You will have to replace the apiKey with the new Map API key you have obtained.
<com.google.android.maps.MapView
android:id=”@+id/mapView”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:enabled=”true” android:clickable=”true”
android:apiKey=”0l4sCTTyRmXTNo7k8DREHvEaLar2UmHGwnhZVHQ”
/>
When you run the application on the emulator or on the device the Google map will be displayed as follows.
But if you haven’t give the internet permission or else the internet connection is low or disabled it will display only a grid instead the map.