Выбрать главу

  super.draw(canvas, mapView, shadow);

  boundCenterBottom(marker);

 }

 @Override

 protected boolean onTap(int i) {

  Toast.makeText(NooYawk.this,

   items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();

  return(true);

 }

 @Override

 public int size() {

  return(items.size());

 }

}

Handling Screen Taps

An Overlay subclass can also implement onTap(), to be notified when the user taps on the map, so the overlay can adjust what it draws. For example, in full-size Google Maps, clicking on a push-pin pops up a bubble with information about the business at that pin’s location. With onTap(), you can do much the same in Android.

The onTap() method for ItemizedOverlay receives the index of the OverlayItem that was clicked. It is up to you to do something worthwhile with this event.

In the case of SitesOverlay, as previously shown, onTap() looks like this:

@Override

protected boolean onTap(int i) {

 Toast.makeText(NooYawk.this,

  items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();

 return(true);

}

Here, we just toss up a short Toast with the “snippet” from the OverlayItem, returning true to indicate we handled the tap.

My, Myself, and MyLocationOverlay

Android has a built-in overlay to handle two common scenarios:

• Showing where you are on the map, based on GPS or other location-providing logic

• Showing where you are pointed, based on the built-in compass sensor, where available

All you need to do is create a MyLocationOverlay instance, add it to your MapView’s list of overlays, and enable and disable the desired features at appropriate times.

The “at appropriate times” notion is for maximizing battery life. There is no sense in updating locations or directions when the activity is paused, so it is recommended that you enable these features in onResume() and disable them in onPause().

For example, NooYawk will display a compass rose using MyLocationOverlay. To do this, we first need to create the overlay and add it to the list of overlays:

me = new MyLocationOverlay(this, map);

map.getOverlays().add(me);

Then, we enable and disable the compass rose as appropriate:

@Override

public void onResume() {

 super.onResume();

 me.enableCompass();

}

@Override

public void onPause() {

 super.onPause();

 me.disableCompass();

}

The Key to It All

If you actually download the source code for the book, compile the NooYawk project, install it in your emulator, and run it, you will probably see a screen with a grid and a couple of pushpins, but no actual maps.

That’s because the API key in the source code is invalid for your development machine. Instead, you will need to generate your own API key(s) for use with your application.

Full instructions for generating API keys, for development and production use, can be found on the Android Web site.[33] In the interest of brevity, let’s focus on the narrow case of getting NooYawk running in your emulator. Doing this requires the following steps:

1. Visit the API key signup page and review the terms of service.

2. Re-read those terms of service and make really, really sure you want to agree to them.

3. Find the MD5 digest of the certificate used for signing your debug-mode applications (described in detail in the following).

4. On the API key signup page, paste in that MD5 signature and submit the form.

5. On the resulting page, copy the API key and paste it as the value of apiKey in your MapView-using layout.

The trickiest part is finding the MD5 signature of the certificate used for signing your debug-mode applications… and much of the complexity is merely in making sense of the concept.

All Android applications are signed using a digital signature generated from a certificate. You are automatically given a debug certificate when you set up the SDK, and there is a separate process for creating a self-signed certificate for use in your production applications. This signature process involves the use of the Java keytool and jarsigner utilities. For the purposes of getting your API key, you only need to worry about keytool.

To get your MD5 digest of your debug certificate, if you are on OS X or Linux, use the following command:

keytool -list -alias androiddebugkey -keystore ~/.android/debug.keystore -storepass

android -keypass android

On other development platforms, you will need to replace the value of the -keystore switch with the location for your platform and user account:

• Windows XP: C:\Documents and Settings\<user>\Local Settings\Application Data\Android\debug.keystore

• Windows Vista: C:\Users\<user>\AppData\Local\Android\debug.keystore

(where <user> is your account name)

The second line of the output contains your MD5 digest, as a series of pairs of hex digits separated by colons.

CHAPTER 35

Handling Telephone Calls

Many, if not most, Android devices will be phones. As such, not only will users be expecting to place and receive calls using Android, but you will have the opportunity to help them place calls, if you wish.

Why might you want to?

• Maybe you are writing an Android interface to a sales management application (a la Salesforce.com) and you want to offer users the ability to call prospects with a single button click, and without them having to keep those contacts both in your application and in the phone’s contacts application

• Maybe you are writing a social networking application, and the roster of phone numbers that you can access shifts constantly, so rather than try to “sync” the social network contacts with the phone’s contact database, you let people place calls directly from your application

вернуться

33

http://code.google.com/android/toolbox/apis/mapkey.html