Android - API Google Maps - Using MapView

This is the Tuto 2 of the Android tutorial series. You can find the application made here on the Android Market.
Just follow the link on your right.

The MapView class of the Android Google Maps API is designed to display a map with data obtained from the Google Maps services.
If you never created a Google Maps application on Android, and for a full description and files needed, take a look at this tutorial:
https://www.badprog.com/android-application-using-google-maps

Let's see the Tuto_2_BadprogTutorialMapViewActivity JAVA class:

// src/com.badprog.android.tutorial.api.googlemaps.mapview

package com.badprog.android.tutorial.api.googlemaps.mapview;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class Tuto_2_BadprogTutorialMapViewActivity
    extends MapActivity
{
    private MapView mapView;
    private Button button1;
    private Button button2;
    
    public static final boolean TRUE = true;
    public static final boolean FALSE = false;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        

        // MapView
        mapView = (MapView) findViewById(R.id.mapview);
    
        // setBuiltInZoomControls
        mapView.setBuiltInZoomControls(TRUE);
        
        // setStreetView
        mapView.setStreetView(TRUE);
        
        // Button 1
        button1 = (Button) findViewById(R.id.button1);
        // getMaxZoomLevel
        button1.setText(String.valueOf(this.mapView.getMaxZoomLevel()));

        // Button 2 // getZoomLevel
        button2 = (Button) findViewById(R.id.button2);
        button2. setText("Zoom Level");
        button2.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                button1.setText(String.valueOf(mapView.getZoomLevel()));
            }
        });
        
        // Button 3 // getMapCenter
        final Button button3 = (Button) findViewById(R.id.button3);
        button3.setText("Center-point");
        button3.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                button1.setText(String.valueOf(mapView.getMapCenter()));
            }
        });

        // Button 4 // getController
        final Button button4 = (Button) findViewById(R.id.button4);
        button4.setText("Zoom in");
        button4.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                button1.setText(String.valueOf(mapView.getController().zoomIn()));
            }
        });

        // Button 5 // isSatellite + setSatellite
        final Button button5 = (Button) findViewById(R.id.button5);
        button5.setText("Satellite is OFF");
        button5.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
                if (mapView.isSatellite()) {
                    button5.setText("Classic");
                    mapView.setSatellite(FALSE);
                } else {
                    button5.setText("Satellite");
                    mapView.setSatellite(TRUE);
                }
            }
        });
        
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

And the main.xml file. Do not forget to replace YOUR_GOOGLE_MAPS_API_KEY by yours:

<?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"
    android:id="@+id/main"
    >
    
    <!-- MapView -->
    <com.google.android.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="YOUR_GOOGLE_MAPS_API_KEY"
    />
    
    <!-- Button 1 -->
    <Button
        android:id="@+id/button1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
       </Button>
    
    <!-- Button 2 -->
    <Button
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/button1"
        >
       </Button>
      
    <!-- Button 3 -->
    <Button
        android:id="@+id/button3"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/button2"
        >
       </Button>
    
    <!-- Button 4 -->
    <Button
        android:id="@+id/button4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/button1"
        >
       </Button>
    
    <!-- Button 5 -->
    <Button
        android:id="@+id/button5"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/button4"
        >
       </Button>
</RelativeLayout>

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.