public class MapManager {
//==============================Singleton===========================================
private volatile static MapManager manager;
private LocationManager lm;
private MapController mapController;
private GeoPoint currentPoint;
private MapManager(Context c){
lm = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
}
public static MapManager getInstance(Context c){
if(manager == null){
synchronized (MapManager.class) {
if(manager == null){
manager = new MapManager(c);
}
}
}//if
return manager;
}
//=====================================监听器=============================================================
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location loc) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
// log it when the location changes
if (loc != null) {
GeoPoint myPoint = new GeoPoint((int)loc.getLatitude()*1000000, (int)loc.getLongitude()*1000000);
mapController.setCenter(myPoint);
}
}
public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
}
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
};
[color=blue]//*********************************对外的接口*******************************************************[/color]
public void startAutoUpdate(){
lm.requestLocationUpdates("gps", 0, 1000, locationListener);
}
public void setCurrentLocToCenter(MapController mapController){
this.mapController = mapController;
//得到当前位置信息,设置在地图上
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
currentPoint = new GeoPoint((int)loc.getLatitude()*1000000, (int)loc.getLongitude()*1000000);
mapController.setCenter(currentPoint);
}
public void addCurrentOverLapToMap(MapView mapView){
//绘制提示信息
MyOverlay mo = new MyOverlay(currentPoint);
mo.onTap(currentPoint, mapView);
mapView.getOverlays().add(mo);
}
//===================================悬浮层定义=============================================================
protected class MyOverlay extends Overlay{
private GeoPoint point;
public MyOverlay(GeoPoint point){
this.point = point;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
//转换地图的经纬度到像素 Geopoint ----> point
Point screenXY = new Point();
mapView.getProjection().toPixels(point, screenXY);
String warning = "I'm here";
//绘制一个方框
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setAntiAlias(true);
//p.setColor(Color.BLUE);
p.setARGB(128, 255, 0, 0);
float warningWidth = p.measureText(warning);
float width = warningWidth + 0.5f;
float high = 25;
RectF roundRect = new RectF(screenXY.x-width/2-2, screenXY.y, screenXY.x+width/2+2, screenXY.y+high);
canvas.drawRoundRect(roundRect, 5, 5, p);
//p.setARGB(255, 51, 51, 255);
//绘制文字“ I'm here ”
p.setColor(Color.BLUE);
canvas.drawText(warning, screenXY.x-width/2-2, screenXY.y+high+1, p);
p.setColor(Color.GREEN);
canvas.drawCircle(screenXY.x, screenXY.y, 3, p);
}
}
}
//==============================Singleton===========================================
private volatile static MapManager manager;
private LocationManager lm;
private MapController mapController;
private GeoPoint currentPoint;
private MapManager(Context c){
lm = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
}
public static MapManager getInstance(Context c){
if(manager == null){
synchronized (MapManager.class) {
if(manager == null){
manager = new MapManager(c);
}
}
}//if
return manager;
}
//=====================================监听器=============================================================
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location loc) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
// log it when the location changes
if (loc != null) {
GeoPoint myPoint = new GeoPoint((int)loc.getLatitude()*1000000, (int)loc.getLongitude()*1000000);
mapController.setCenter(myPoint);
}
}
public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
}
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
};
[color=blue]//*********************************对外的接口*******************************************************[/color]
public void startAutoUpdate(){
lm.requestLocationUpdates("gps", 0, 1000, locationListener);
}
public void setCurrentLocToCenter(MapController mapController){
this.mapController = mapController;
//得到当前位置信息,设置在地图上
Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
currentPoint = new GeoPoint((int)loc.getLatitude()*1000000, (int)loc.getLongitude()*1000000);
mapController.setCenter(currentPoint);
}
public void addCurrentOverLapToMap(MapView mapView){
//绘制提示信息
MyOverlay mo = new MyOverlay(currentPoint);
mo.onTap(currentPoint, mapView);
mapView.getOverlays().add(mo);
}
//===================================悬浮层定义=============================================================
protected class MyOverlay extends Overlay{
private GeoPoint point;
public MyOverlay(GeoPoint point){
this.point = point;
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
//转换地图的经纬度到像素 Geopoint ----> point
Point screenXY = new Point();
mapView.getProjection().toPixels(point, screenXY);
String warning = "I'm here";
//绘制一个方框
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setAntiAlias(true);
//p.setColor(Color.BLUE);
p.setARGB(128, 255, 0, 0);
float warningWidth = p.measureText(warning);
float width = warningWidth + 0.5f;
float high = 25;
RectF roundRect = new RectF(screenXY.x-width/2-2, screenXY.y, screenXY.x+width/2+2, screenXY.y+high);
canvas.drawRoundRect(roundRect, 5, 5, p);
//p.setARGB(255, 51, 51, 255);
//绘制文字“ I'm here ”
p.setColor(Color.BLUE);
canvas.drawText(warning, screenXY.x-width/2-2, screenXY.y+high+1, p);
p.setColor(Color.GREEN);
canvas.drawCircle(screenXY.x, screenXY.y, 3, p);
}
}
}