Creating the Friends Map Activity

The FriendsMap Activity is the final Activity that will be callable from the main application. This Activity will call your recordset from the Friends database and draw a circle on a Google Map for each friend. The Activity will also draw a circle for you at your current location.

You need to begin by adding two new files to your project, friendsmap.xml and FriendsMap.java. Because you have seen the layout for the friendsmap.xml file in Chapter 9, there is no need to fully explain it here. You are using a RelativeLayout to place four Buttons over a Google Map. The full friendsmap.xml file should look like this:

<?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"

<view class="com.google.android.maps.MapView" android:id="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/buttonZoomIn" style="?android:attr/buttonStyleSmall" android:text="+"

android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonMapView" style="?android:attr/buttonStyleSmall" android:text="Map"

android:layout_alignRight="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonSatView" style="?android:attr/buttonStyleSmall" android:text="Sat"

android:layout_alignRight="@+id/myMap"

android:layout_alignBottom="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/buttonZoomOut" style="?android:attr/buttonStyleSmall" android:text="-"

android:layout_alignBottom="@+id/myMap" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>

Because you have seen the overwhelming majority of the FriendsMap.java file in Chapter 9 as well, I will not go over every little detail. However, there is one method that should be explained.

You will create a method called LoadFriends( ) that will access the database, read the records, and draw the Overlay. Take a look at the LoadFriends( ) code that follows. Notice that you open the database, match and parse the location field, create a point from the latitude and longitude in the location field, and draw that point to the Overlay. The last thing the method does is to grab your coordinates from the GPS and draw them to the Overlay with the label "ME".

public void LoadFriends(MapView mv, MapController mc, Cursor c){ Point myLocation = null; Double latPoint = null; Double lngPoint = null; c.first(); do{

if (c.getString(c.getColumnIndex("location")) != null) { final String geoPattern = "(geo:[\\-]?[0-9]{1,3}\\.[0 9]{1,6}\\,[\\-]?[0-9]{1,3}\\.[0-9]{1,6}\\#)";

Pattern pattern = Pattern.compile(geoPattern);

CharSequence inputStr = c.getString(c.getColumnIndex("location"));

Matcher matcher = Pattern.matcher(inputStr);

boolean matchFound = matcher.find(); if (matchFound) {

String groupStr = matcher.group(0); latPoint =

Double.valueOf(groupStr.substring(groupStr.indexOf(":") + 1, groupStr.indexOf(","))) ;

lngPoint =

Double.valueOf(groupStr.substring(groupStr.indexOf(",") + 1, groupStr.indexOf("#"))) ; Point friendLocation = new Point(latPoint.intValue(),lngPoint.intValue());

drawFriendsOverlay.addNewFriend(c.getString(c.getColumnIndex("name")), friendLocation);

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

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

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

myLocation = new Point(myLatPoint.intValue(),myLngPoint.intValue()); drawFriendsOverlay.addNewFriend("Me", myLocation);

mc.centerMapTo(myLocation, false); mc.zoomTo(9); mv = null;

The remainder of the FriendsMap.java file operates the zoom and toggle buttons, as introduced in Chapter 10:

package android_programmers_guide.FindAFriend;

import android.os.Bundle;

import android.location.LocationManager;

import android.view.View;

import android.content.Context;

import android.content.Intent;

import android.database.Cursor;

import android.widget.Button;

import java.util.regex.Pattern;

import java.util.regex.Matcher;

import android.graphics.Canvas;

import android.graphics.RectF;

import android.graphics.Paint;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import com.google.android.maps.Point;

import com.google.android.maps.MapController;

import com.google.android.maps.Overlay;

import com.google.android.maps.OverlayController;

public class FriendsMap extends MapActivity {

private static final String[] PROJECTION = new String[] {

Friends.Friend.NAME, Friends.Friend.LOCATION}; public Cursor mCursor;

DrawFriendsOverlay drawFriendsOverlay = new DrawFriendsOverlay(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.friendsmap);

Intent intent = getIntent(); if (intent.getData() == null) {

intent.setData(Friends.Friend. CONTENT_URI);

mCursor = managedQuery(getIntent().getData(), PROJECTION, null,null);

final MapView myMap = (MapView) findViewById(R.id.myMap); final MapController myMapController = myMap.getController(); LoadFriends(myMap, myMapController, mCursor); OverlayController myOverlayController = myMap.createOverlayController();

myOverlayController.add(drawFriendsOverlay, true);

final Button zoomIn = (Button) findViewById(R.id.buttonZoomIn);

zoomIn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v){

ZoomIn(myMap,myMapController);

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

ZoomOut(myMap,myMapController);

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

ShowMap(myMap,myMapController);

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

ShowSat(myMap,myMapController);

public void LoadFriends(MapView mv, MapController mc, Cursor c){ Point myLocation = null; Double latPoint = null; Double lngPoint = null; c.first(); do{

if (c.getString(c.getColumnIndex("location")) != null) { final String geoPattern = "(geo:[\\-]?[0-9]{l,3}\\.[0 9]{l,6}\\,[\\-]?[0-9]{l,3}\\.[0-9]{l,6}\\#)";

Pattern pattern = Pattern.compile(geoPattern);

CharSequence inputStr = c.getString(c.getColumnIndex("location"));

Matcher matcher = pattern.matcher(inputStr);

boolean matchFound = matcher.find(); if (matchFound) {

String groupStr = matcher.group(0); latPoint =

Double.valueOf(groupStr.substring(groupStr.indexOf(":") + 1, groupStr.indexOf(","))) ;

lngPoint =

Double.valueOf(groupStr.substring(groupStr.indexOf(",") + 1, groupStr.indexOf("#"))) ; Point friendLocation = new Point(latPoint.intValue(),lngPoint.intValue());

drawFriendsOverlay.addNewFriend(c.getString(c.getColumnIndex("name")), friendLocation);

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

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

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

myLocation = new Point(myLatPoint.intValue(),myLngPoint.intValue()); drawFriendsOverlay.addNewFriend("Me", myLocation);

mc.centerMapTo(myLocation, false); mc.zoomTo(9); mv = null;

public void ZoomIn(MapView mv, MapController mc){ if(mv.getZoomLevel()!=21){

public void ZoomOut(MapView mv, MapController mc){ if(mv.getZoomLevel()!=1){

public void ShowMap(MapView mv, MapController mc){ if (mv.isSatellite()){

mv.toggleSatellite();

public void ShowSat(MapView mv, MapController mc){ if (!mv.isSatellite()){

mv.toggleSatellite();

protected class DrawFriendsOverlay extends Overlay{ public String[] friendName = new String[0]; public Point[] friendPoint = new Point[0]; final Paint paint = new Paint();

@Override public void draw(Canvas canvas, PixelCalculator calculator, Boolean shadow){

for(int x=0;x<friendPoint.length; x++){ int[] coords = new int[2];

calculator.getPointXY(friendPoint[x], coords); RectF oval = new RectF(coords[0] - 7, coords[1] + 7, coords[0] + 7, coords[1] - 7); paint.setTextSize(14);

canvas.drawText(friendName[x], coords[0] +9, coords[1], paint);

canvas.drawOval(oval, paint); }

public void addNewFriend(String name,Point point ){ int x = friendPoint.length;

String[] friendNameB = new String[x + 1]; Point[] friendPointB = new Point[x + 1];

System.arraycopy(friendName, 0, friendNameB, 0, x ); System.arraycopy(friendPoint, 0, friendPointB, 0, x);

friendNameB[x] = name; friendPointB[x]= point;

friendName = new String[x + 1]; friendPoint = new Point[x + 1];

System.arraycopy(friendNameB, 0, friendName, 0, x + 1 ); System.arraycopy(friendPointB, 0, friendPoint, 0, x + 1 );

The last task to finish this project is to create the main Activity, FindAFriend, which will be a shell that calls the other Activities you created in this chapter.

0 0

Post a comment