Maps,Geocoding,and Location-Based Services

SELECTING A LOCATION PROVIDER

To get an instance of a specific provider, call getProvider, passing in the name:
String providerName = LocationManager.GPS_PROVIDER;
LocationProvider gpsProvider;
gpsProvider = locationManager.getProvider(providerName);

 

Finding the Available Providers

The LocationManager class includes static string constants that return the provider name for the two
most common Location Providers:
➤ LocationManager.GPS_PROVIDER
➤ LocationManager.NETWORK_PROVIDER
To get a list of names for all the providers available on the device, call getProviders, using a Boolean
to indicate if you want all, or only the enabled, providers to be returned:
boolean enabledOnly = true;
List<String> providers = locationManager.getProviders(enabledOnly);

 

Finding Location Providers Using Criteria

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);

 

String bestProvider = locationManager.getBestProvider(criteria, true);

 

If more than one Location Provider matches your criteria, the one with the greatest accuracy is returned.
If no Location Providers meet your requirements the criteria are loosened, in the following order, until
a provider is found:
➤ Power use
➤ Accuracy
➤ Ability to return bearing, speed, and altitude

 

FINDING YOUR LOCATION

String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(serviceString);

 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

 

String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);

 

Tracking Movement

Use the requestLocationUpdates method to get updates whenever the current location changes, using
a LocationListener.

 

locationManager.requestLocationUpdates(provider, t, distance,
myLocationListener);

locationManager.removeUpdates(myLocationListener);

 

 Reverse Geocoding

 

location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
List<Address> addresses = null;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
addresses = gc.getFromLocation(latitude, longitude, 10);
} catch (IOException e) {}

 

 

Forward Geocoding 

List<Address> result = geocoder.getFromLocationName(aStreetAddress, maxResults);

Geocoder fwdGeocoder = new Geocoder(this, Locale.US);
String streetAddress = "160 Riverside Drive, New York, New York";
List<Address> locations = null;
try {
locations = fwdGeocoder.getFromLocationName(streetAddress, 10);
} catch (IOException e) {}

List<Address> locations = null;
try {
locations = fwdGeocoder.getFromLocationName(streetAddress, 10,
n, e, s, w);
} catch (IOException e) {}

 

 

 

Introducing Map View and Map Activity

MapView is theMap View control

 

MapActivity is the base class you extend to create a new Activity that can include a Map
View. The MapActivity class handles the application life cycle and background service
management required for displaying maps. As a result you can use Map Views only within
MapActivity-derived Activities.

 

Overlay is the class used to annotate your maps. Using Overlays, you can use a Canvas to
draw onto any number of layers that are displayed on top of a Map View.

 

MapController is used to control the map, enabling you to set the center location and zoom
levels.
➤ MyLocationOverlay is a special Overlay that can be used to display the current position and
orientation of the device.
➤ ItemizedOverlays and OverlayItems are used together to let you create a layer of map markers,
displayed using Drawables and associated text.

 

mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.setTraffic(true);

 

int maxZoom = mapView.getMaxZoomLevel();
GeoPoint center = mapView.getMapCenter();
int latSpan = mapView.getLatitudeSpan();
int longSpan = mapView.getLongitudeSpan();

 

mapView.setBuiltInZoomControls(true);

 

Using the Map Controller

 

MapController mapController = myMapView.getController();

Double lat = 37.422006*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

mapController.setCenter(point);
mapController.setZoom(1);

 

mapController.animateTo(point);

 

 

 

 

Creating and Using Overlays

import android.graphics.Canvas;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class MyOverlay extends Overlay {
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (shadow == false) {
[ . . . Draw annotations on main map layer . . . ]
}
else {
[ . . . Draw annotations on the shadow layer . . . ]
}
}
@Override
public boolean onTap(GeoPoint point, MapView mapView) {

// Return true if screen tap is handled by this overlay
return false;
}
}

 

Introducing Projections

Projection projection = mapView.getProjection();

Point myPoint = new Point();
// To screen coordinates
projection.toPixels(geoPoint, myPoint);
// To GeoPoint location coordinates
projection.fromPixels(myPoint.x, myPoint.y);

 

Drawing on the Overlay Canvas

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Double lat = -31.960906*1E6;
Double lng = 115.844822*1E6;
GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());
if (shadow == false) {
Point myPoint = new Point();
projection.toPixels(geoPoint, myPoint);
// Create and setup your paint brush
Paint paint = new Paint();
paint.setARGB(250, 255, 0, 0);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
// Create the circle
int rad = 5;
RectF oval = new RectF(myPoint.x-rad, myPoint.y-rad,
myPoint.x+rad, myPoint.y+rad);

 

// Draw on the canvas
canvas.drawOval(oval, paint);
canvas.drawText("Red Circle", myPoint.x+rad, myPoint.y, paint);
}
}

 

Handling Map Tap Events

The onTap handler receives two parameters:
➤ A GeoPoint that contains the latitude/longitude of the map location tapped
➤ The MapView that was tapped to trigger this event
When you are overriding onTap, the method should return true if it has handled a particular tap and
false to let another Overlay handle it

 

@Override
public boolean onTap(GeoPoint point, MapView mapView) {
// Perform hit test to see if this overlay is handling the click
if ([ . . . perform hit test . . . ]) {
[ . . . execute on tap functionality . . . ]
return true;
}

 

// If not handled return false
return false;
}

 

Adding and Removing Overlays

List<Overlay> overlays = mapView.getOverlays();

List<Overlay> overlays = mapView.getOverlays();
MyOverlay myOverlay = new MyOverlay();
overlays.add(myOverlay);
mapView.postInvalidate();

 

 

Creating a new Itemized Overlay

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
populate();
}
@Override
protected OverlayItem createItem(int index) {
switch (index) {
case 1:
Double lat = 37.422006*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
OverlayItem oi;
oi = new OverlayItem(point, "Marker", "Marker Text");
return oi;
}
return null;
}
@Override
public int size() {
// Return the number of markers in the collection
return 1;
}
}

 

 

List<Overlay> overlays = mapView.getOverlays();
MyItemizedOverlay markers = new
MyItemizedOverlay(r.getDrawable(R.drawable.marker));
overlays.add(markers);

 

Skeleton code for a dynamic Itemized Overlay

public class MyDynamicItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> items;
public MyDynamicItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
items = new ArrayList<OverlayItem>();
populate();
}
public void addNewItem(GeoPoint location, String markerText,
String snippet) {
items.add(new OverlayItem(location, markerText, snippet));
populate();
}
public void removeItem(int index) {
items.remove(index);
populate();
}
@Override
protected OverlayItem createItem(int index) {
return items.get(index);
}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值