Passing Coordinates to Google Maps

In this section, you will build on the Activity you created in the previous section. The major modification you will make to your AndroidLBS Activity is to pass the coordinates to Google Maps. You will use Google Maps to display the user's current location.

The only change you need to make to your main.xml file is to add a layout for the MapView. In the current version of the Android SDK, the MapView is established as a generic View. Perhaps in a future release there will be a MapView that corresponds to this layout.

<view class="com.google.android.maps.MapView" android:id="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

The complete main.xml file should look like this:

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

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

android:layout_height="fill_parent"

<Button android:id="@+id/gpsButton" android:layout_width="fill_parent" android:layout_height="wrap_content"

android:text="Where Am I"

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width="wrap_content"

android:layout_height="wrap_content"

<TextView android:id="@+id/latLabel" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="Latitude: 11

<TextView android:id="@+id/latText" android:layout_width="wrap_content"

android:layout_height="wrap_content"

</LinearLayout>

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width="wrap_content"

android:layout_height="wrap_content"

<TextView android:id="@+id/lngLabel" android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="Longitude: 11

<TextView android:id="@+id/lngText" android:layout_width="wrap_content"

android:layout_height="wrap_content"

</LinearLayout>

<view class="com.google.android.maps.MapView" android:id="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</LinearLayout>

Because you are embedding the MapView within this Activity, you need to change the definition of your class. Currently, your main class extends Activity. However, to properly work with the Google MapView, you must extend MapActivity. Therefore, you need to import the MapActivity package and replace the Activity package with it in your header. Import the following packages:

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

The Point package will be used to hold point values that represent map coordinates, whereas the MapController will center the map to your Point. These two packages are critical for using the MapView.

Now you are ready to add the code that will establish the map and pass your coordinates to it. First, set up a MapView and assign it the layout from main.xml:

MapView myMap = (MapView) findViewById(R.id.myMap);

Next, set up a Point and assign it the latPoint and lngPoint values that you retrieved from the GPS:

Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());

Now you can create your MapController, which will be used to move the focus of the Google Map to the location you just defined in the Point. Use the getController( ) method from the MapView to establish a controller in your specific Map:

MapController myMapController = myMap.getController();

The only job left is to use the controller to move the map to your location (to make the map a little more recognizable, set the zoom to 9):

myMapController.centerMapTo(myLocation, false); myMapController.zoomTo(9);

What you have just written is all the code needed to utilize Google Maps from your Activity. The full class should look like this:

package android_programmers_guide.AndroidLBS;

import android.os.Bundle;

import android.location.LocationManager;

import android.view.View;

import android.widget.TextView;

import android.content.Context;

import android.widget.Button;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import com.google.android.maps.Point;

import com.google.android.maps.MapController;

public class AndroidLBS extends MapActivity {

/** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

final Button gpsButton = (Button) findViewById(R.id.gpsButton); gpsButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){ LoadProviders();

public void LoadProviders(){

TextView latText = (TextView) findViewById(R.id.latText);

TextView lngText = (TextView) findViewById(R.id.lngText);

LocationManager myManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Double latPoint = myManager.getCurrentLocation("gps").getLatitude()*1E6;

Double lngPoint = myManager.getCurrentLocation("gps").getLongitude()*1E6;

latText.setText(latPoint.toString());

lngText.setText(lngPoint.toString());

MapView myMap = (MapView) findViewById(R.id.myMap);

Point myLocation = new Point(latPoint.intValue(),lngPoint.intValue());

MapController myMapController = myMap.getController(); myMapController.centerMapTo(myLocation, false); myMapController.zoomTo(9);

Run the Activity in the Emulator. The Activity should open to a blank map, as shown in the following illustration.

Click the Where Am I button and you should see the map focus to, and zoom in on, San Francisco. Take a look at the following illustration to see how your map should appear.

0 0

Post a comment