It is up to you to arrange for an activity or intent receiver to respond to the Intent you register with the proximity alert. What you then do when the Intent arrives is up to you: set up a notification (e.g., vibrate the device), log the information to a content provider, post a message to a Web site, etc. Note that you will receive the Intent whenever the position is sampled and you are within the target zone — not just upon entering the zone. Hence, you will get the Intent several times, perhaps quite a few times depending on the size of the target zone and the speed of the device’s movement.

Testing… Testing…

The Android emulator does not have the ability to get a fix from GPS, triangulate your position from cell towers, or identify your location by some nearby WiFi signal. So, if you want to simulate a moving device, you will need to have some means of providing mock location data to the emulator.

For whatever reason, this particular area has undergone significant changes as Android itself has evolved. It used to be that you could provide mock location data within your application, which was very handy for demonstration purposes. Alas, those options have all been removed as of Android 1.0.

One likely option for supplying mock location data is the Dalvik Debug Monitor Service (DDMS). This is an external program, separate from the emulator, which can feed the emulator single location points or full routes to traverse, in a few different formats. DDMS is described in greater detail in Chapter 37.

CHAPTER 34

Mapping with MapView and MapActivity

One of Google’s most popular services — after search, of course — is Google Maps, where you can find everything from the nearest pizza parlor to directions from New York City to San Francisco (only 2,905 miles!) to street views and satellite imagery.

Android, not surprisingly, integrates Google Maps. There is a mapping activity available to users straight off the main Android launcher. More relevant to you, as a developer, are MapView and MapActivity, which allow you to integrate maps into your own applications. Not only can you display maps, control the zoom level, and allow people to pan around, but you can tie in Android’s location-based services to show where the device is and where it is going.

Fortunately, integrating basic mapping features into your Android project is fairly easy. However, there is a fair bit of power available to you, if you want to get fancy.

Terms, Not of Endearment

Google Maps, particularly when integrated into third party applications, requires agreeing to a fairly lengthy set of legal terms. These terms include clauses that you may find unpalatable.

If you are considering Google Maps, please review these terms closely to determine if your intended use will not run afoul of any clauses. You are strongly recommended to seek professional legal counsel if there are any potential areas of conflict.

Also, keep your eyes peeled for other mapping options, based off of other sources of map data, such as OpenStreetMap.[32]

The Bare Bones

Far and away the simplest way to get a map into your application is to create your own subclass of MapActivity. Like ListActivity, which wraps up some of the smarts behind having an activity dominated by a ListView, MapActivity handles some of the nuances of setting up an activity dominated by a MapView.

In your layout for the MapActivity subclass, you need to add an element named, at the time of this writing, com.google.android.maps.MapView. This is the “longhand” way to spell out the names of widget classes, by including the full package name along with the class name. This is necessary because MapView is not in the com.google.android.widget namespace. You can give the MapView widget whatever android:id attribute value you want, plus handle all the layout details to have it render properly alongside your other widgets.

However, you do need to have:

• android:apiKey, which in production will need to be a Google Maps API key — more on this here

• android:clickable='true', if you want users to be able to click and pan through your map

For example, from the Maps/NooYawk sample application, here is the main layout:

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

<RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'

 android:layout_width='fill_parent'

 android:layout_height='fill_parent'>

 <com.google.android.maps.MapView android:id='@+id/map'

  android:layout_width='fill_parent'

  android:layout_height='fill_parent'

  android:apiKey='<YOUR_API_KEY>'

  android:clickable='true' />

 <LinearLayout android:id='@+id/zoom'

  android:layout_width='wrap_content'

  android:layout_height='wrap_content'

  android:layout_alignParentBottom='true'

  android:layout_alignParentLeft='true' />

</RelativeLayout>

We’ll cover that mysterious zoom LinearLayout and the apiKey in later sections of this chapter. In addition, you will need a couple of extra things in your AndroidManifest.xml file:

• The INTERNET and ACCESS_COARSE_LOCATION permissions

• Inside your <application>, a <uses-library> element with android:name='com.google.android.maps', to indicate you are using one of the optional Android APIs

Here is the AndroidManifest.xml file for NooYawk:

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

<manifest xmlns:android='http://schemas.android.com/apk/res/android'

 package='com.commonsware.android.maps'>

 <uses-permission android:name='android.permission.INTERNET' />

 <uses-permission android:name='android.permission.ACCESS_COARSE_LOCATION'

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату