» 首頁 » 討論區 » Android程式設計 »趨近警告使用

趨近警告使用

發表人: 訪客
發表時間: 2012-08-23 11:55:19
我想要寫一個畫面是位於GOOGLE MAP上
然後點螢幕畫面之後顯示經緯座標並附帶一個確認button
點下確認後開始判斷

我已經寫出定位跟點擊畫面顯示出經緯度
但趨近警告不太會加
可以請各位幫個忙嗎!
謝謝~
發表人: 訪客
發表時間: 2012-08-23 13:15:01
[sea:javaCode]
public class MainActivity extends MapActivity implements LocationListener{
static final int INITIAL_ZOOM_LEVEL = 16;
static final int INITIAL_LATITUDE = 25040255;
static final int INITIAL_LONGITUDE = 121512377;
private static final String PROXIMITY_ALERT_ACTION_NAME = "mx.android.ch14.ProximityAlert";
private LocationManager mLocationManager;
private Location mml;
MapView mapView;
TextView tv;
List<Overlay> mapOverlays;
Drawable drawable;
HelloItemizedOverlay itemizedOverlay;
private List<GeoPoint> updatePoints = new ArrayList<GeoPoint>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// mml = getLocationProvider(mLocationManager);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

//設定MapView可以縮放
//tv = (TextView) findViewById(R.id.tv);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);


}


@Override
protected void onResume() {
if (mLocationManager != null) {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
this);
}
super.onResume();
}
//在Pause階段關閉mLocationListener介面,不再獲得地理位置的更新資料
@Override
protected void onPause() {
if (mLocationManager != null) {
mLocationManager.removeUpdates(this);
}
super.onPause();
}


protected boolean isRouteDisplayed() {
return false;
}

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat=location.getLatitude()*1E6;
double lon=location.getLongitude()*1E6;
MapController mc = mapView.getController();
mc.setZoom(INITIAL_ZOOM_LEVEL);
mc.setCenter(new GeoPoint(((int)lat),((int)lon)));
updatePoints.add(new GeoPoint((int)lat,(int)lon));
//貼上標記
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.holo);
itemizedOverlay = new HelloItemizedOverlay(updatePoints,drawable, this);
GeoPoint point = new GeoPoint((int)lat,(int)lon);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
switch (status) {
case LocationProvider.AVAILABLE:
Log.v("Status", "AVAILABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.v("Status", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v("Status", "TEMPORARILY_UNAVAILABLE");
break;
}
}
}
[/sea]
發表人: 訪客
發表時間: 2012-08-23 13:15:39
[sea:javaCode]
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context context;
public double lat;
public double lon;
Drawable drawable;
List<Overlay> mapOverlays;
private List<GeoPoint> updatePoints;
public HelloItemizedOverlay(List<GeoPoint> points,Drawable defaultMarker,Context context) {
super(boundCenterBottom(defaultMarker));
this.context=context;
this.updatePoints = points;
// TODO Auto-generated constructor stub
}


public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
public boolean onTap(GeoPoint p, MapView mapView) {
// TODO Auto-generated method stub
lat=p.getLatitudeE6();
lon=p.getLongitudeE6();
Toast.makeText(context, "緯度:"+lat/1000000+"經度:"+lon/1000000, Toast.LENGTH_LONG).show();
mapOverlays = mapView.getOverlays();
drawable = context.getResources().getDrawable(R.drawable.btn);

GeoPoint point = new GeoPoint((int)p.getLatitudeE6(),(int)p.getLongitudeE6());
OverlayItem overlayitem = new OverlayItem(point, "", "");
addOverlay(overlayitem);
return super.onTap(p, mapView);
}


}
[/sea]
發表人: 訪客
發表時間: 2012-08-23 13:17:08
這是我目前打的....
發表人: Seachaos
積分: 2432
發表時間: 2012-08-24 01:10:50
Hi
你可以把你點擊(Tap)的座標記錄下來
然後在
onLocationChanged
這邊比對兩者的距離

就可以做出趨近警告的效果