项目目录
类代码
Mylocation.java
package com.swift.mylocation;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.swift.mylocation.R;
public class Mylocation extends MapActivity {
MapController mapController;
List<Overlay> overlays;
MyPositionOverlay positionOverlay;
LocationManager locationManager;
MapView myMapView;
final String PROXIMITY_ALERT = new String(
"android.intent.action.proximityalert");
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myMapView = (MapView) findViewById(R.id.myMapView);
mapController = myMapView.getController();
myMapView.setSatellite(true);
myMapView.setStreetView(true);
myMapView.displayZoomControls(false);
mapController.setZoom(17);
positionOverlay = new MyPositionOverlay();
overlays = myMapView.getOverlays();
overlays.add(positionOverlay);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(true);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
setProximityAlert();
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
void setProximityAlert() {
double lat = 31.620356666666666;
double lng = 121.38631333333333;
float radius = 50f;
long expiration = -1;
Intent intent = new Intent(PROXIMITY_ALERT);
intent.setAction(PROXIMITY_ALERT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, -1,
intent, 0);
locationManager.addProximityAlert(lat, lng, radius, expiration,
proximityIntent);
}
/** Update UI with a new location */
private void updateWithNewLocation(Location location) {
String latLongString;
TextView myLocationText;
myLocationText = (TextView) findViewById(R.id.myLocationText);
String addressString = "No address found";
if (location != null) {
positionOverlay.setLocation(location);
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.CHINA);
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getCountryName());
sb.append(address.getLocality()).append("\n");
}
addressString = sb.toString();
} catch (IOException e) {
}
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString
+ "\n" + addressString);
Toast.makeText(this,this.getIntent().getStringExtra(PROXIMITY_ALERT) , Toast.LENGTH_LONG);
}
@Override
protected boolean isRouteDisplayed() {
return true;
}
}
MyPostionOverlay.java
package com.swift.mylocation;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class MyPositionOverlay extends Overlay {
Location location, mlocation;
final Double lat = 31.620356666666666 * 1E6,
lon = 121.38631333333333 * 1E6;
final int ccc = 50;
private final int mRadius = 5;
/**
* @return the location
*/
public Location getLocation() {
return location;
}
/**
* @param location
* the location to set
*/
public void setLocation(Location location) {
this.location = location;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
if (shadow == false) {
Double latitude = location.getLatitude() * 1E6;
Double longitude = location.getLongitude() * 1E6;
GeoPoint geoPoint, gp;
geoPoint = new GeoPoint(latitude.intValue(), longitude.intValue());
gp = new GeoPoint(lat.intValue(), lon.intValue());
Point point = new Point();
projection.toPixels(geoPoint, point);
RectF oval = new RectF(point.x - mRadius, point.y - mRadius,
point.x + mRadius, point.y + mRadius);
Paint paint = new Paint();
paint.setARGB(250, 255, 0, 0);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
Paint backPaint = new Paint();
backPaint.setARGB(175, 50, 50, 50);
backPaint.setAntiAlias(true);
RectF backRect = new RectF(point.x + 2 + mRadius, point.y - 3
* mRadius, point.x + 65, point.y + mRadius);
Point center = new Point();
projection.toPixels(gp, center);
Paint ccp = new Paint();
ccp.setARGB(250, 0, 0, 255);
ccp.setAntiAlias(true);
canvas.drawCircle(center.x, center.y, ccc, ccp);
canvas.drawOval(oval, paint);
canvas.drawRoundRect(backRect, 5, 5, backPaint);
canvas.drawText("Here I Am", point.x + 2 * mRadius, point.y, paint);
}
super.draw(canvas, mapView, shadow);
}
@Override
public boolean onTap(GeoPoint point, MapView mapView) {
return false;
}
}
ProximityIntentReceiver.java
package com.swift.mylocation;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
public class ProximityIntentReceiver extends BroadcastReceiver {
private final String textin = "alert : in", textout = "alert : out";
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean isEnter = intent.getBooleanExtra(key, false);
if (isEnter) {
Log.d("allan", textin);
} else {
Log.d("allan", textout);
}
}
}
main.xml
<?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">
<TextView
android:id="@+id/myLocationText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.google.android.maps.MapView
android:id="@+id/myMapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="yourApiKey"
/>
</LinearLayout>
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.swift.mylocation">
<application android:icon="@drawable/icon">
<uses-library android:name="com.google.android.maps"/>
<activity android:name="com.swift.mylocation.Mylocation" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="ProximityIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.proximityalert" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk android:minSdkVersion="3"/>
</manifest>