public
class
NavigationDemoActivity
extends
MapActivity {
private
String mMapKey =
"注冊自己的key"
;
private
EditText destinationEditText =
null
;
private
Button startNaviButton =
null
;
private
MapView mapView =
null
;
private
BMapManager mMapManager =
null
;
private
MyLocationOverlay myLocationOverlay =
null
;
private
LocationListener locationListener;
private
MKSearch searchModel;
GeoPoint pt;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
destinationEditText = (EditText)
this
.findViewById(R.id.et_destination);
startNaviButton = (Button)
this
.findViewById(R.id.btn_navi);
mMapManager =
new
BMapManager(getApplication());
mMapManager.init(mMapKey,
new
MyGeneralListener());
super
.initMapActivity(mMapManager);
mapView = (MapView)
this
.findViewById(R.id.bmapsView);
mapView.setBuiltInZoomControls(
true
);
myLocationOverlay =
new
MyLocationOverlay(
this
, mapView);
mapView.getOverlays().add(myLocationOverlay);
locationListener =
new
LocationListener(){
@Override
public
void
onLocationChanged(Location location) {
if
(location !=
null
){
pt =
new
GeoPoint((
int
)(location.getLatitude()*1e6),
(
int
)(location.getLongitude()*1e6));
mapView.getController().animateTo(pt);
}
}
};
searchModel =
new
MKSearch();
searchModel.setDrivingPolicy(MKSearch.ECAR_DIS_FIRST);
searchModel.init(mMapManager,
new
MKSearchListener() {
@Override
public
void
onGetDrivingRouteResult(MKDrivingRouteResult res,
int
error) {
if
(error !=
0
|| res ==
null
) {
Toast.makeText(NavigationDemoActivity.
this
,
"抱歉,未找到结果"
, Toast.LENGTH_SHORT).show();
return
;
}
RouteOverlay routeOverlay =
new
RouteOverlay(NavigationDemoActivity.
this
, mapView);
MKRoute route = res.getPlan(
0
).getRoute(
0
);
int
distanceM = route.getDistance();
String distanceKm = String.valueOf(distanceM /
1000
) +
"."
+String.valueOf(distanceM %
1000
);
System.out.println(
"距离:"
+distanceKm+
"公里---节点数量:"
+route.getNumSteps());
for
(
int
i =
0
; i < route.getNumSteps(); i++) {
MKStep step = route.getStep(i);
System.out.println(
"节点信息:"
+step.getContent());
}
routeOverlay.setData(route);
mapView.getOverlays().clear();
mapView.getOverlays().add(routeOverlay);
mapView.invalidate();
mapView.getController().animateTo(res.getStart().pt);
}
@Override
public
void
onGetWalkingRouteResult(MKWalkingRouteResult res,
int
error) {
}
@Override
public
void
onGetTransitRouteResult(MKTransitRouteResult arg0,
int
arg1) {
}
@Override
public
void
onGetBusDetailResult(MKBusLineResult arg0,
int
arg1) {
}
@Override
public
void
onGetAddrResult(MKAddrInfo arg0,
int
arg1) {
}
@Override
public
void
onGetSuggestionResult(MKSuggestionResult arg0,
int
arg1) {
}
@Override
public
void
onGetPoiResult(MKPoiResult arg0,
int
arg1,
int
arg2) {
}
});
startNaviButton.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View v) {
String destination = destinationEditText.getText().toString();
MKPlanNode startNode =
new
MKPlanNode();
startNode.pt = pt;
MKPlanNode endNode =
new
MKPlanNode();
endNode.name = destination;
String city = getResources().getString(R.string.beijing);
searchModel.drivingSearch(city, startNode, city, endNode);
}
});
}
@Override
protected
void
onResume() {
mMapManager.getLocationManager().requestLocationUpdates(locationListener);
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
mMapManager.start();
super
.onResume();
}
@Override
protected
void
onPause() {
mMapManager.getLocationManager().removeUpdates(locationListener);
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
mMapManager.stop();
super
.onPause();
}
@Override
protected
boolean
isRouteDisplayed() {
return
false
;
}
class
MyGeneralListener
implements
MKGeneralListener {
@Override
public
void
onGetNetworkState(
int
iError) {
Log.d(
"MyGeneralListener"
,
"onGetNetworkState error is "
+ iError);
Toast.makeText(NavigationDemoActivity.
this
,
"您的网络出错啦!
"
,
Toast.LENGTH_LONG).show();
}
@Override
public
void
onGetPermissionState(
int
iError) {
Log.d(
"MyGeneralListener"
,
"onGetPermissionState error is "
+ iError);
if
(iError == MKEvent.ERROR_PERMISSION_DENIED) {
Toast.makeText(NavigationDemoActivity.
this
,
"请在BMapApiDemoApp.java文件输入正确的授权Key!
"
,
Toast.LENGTH_LONG).show();
}
}
}
}