2
回答
求助一个关于百度API 中gps的问题,一整天就一直意外停止,谢谢了。
package src.com;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKLocationManager;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.RouteOverlay;
import com.baidu.mapapi.TransitOverlay;

public class RoutePlan extends MapActivity {
Button mBtnDrive = null; // 驾车搜索
Button mBtnTransit = null; // 公交搜索
Button mBtnWalk = null; // 步行搜索
MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
MapView mMapView; // 地图View
BMapApiDemoApp app;
OverlayT overitem;
//private int onceMyPoint = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.routeplan);
app = (BMapApiDemoApp)this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener());
}
app.mBMapMan.start();
        // 如果使用地图SDK,请初始化地图Activity
        super.initMapActivity(app.mBMapMan);
        
        mMapView = (MapView)findViewById(R.id.bmapView);
        mMapView.setBuiltInZoomControls(true);
        //设置在缩放动画过程中也显示overlay,默认为不绘制
        mMapView.setDrawOverlayWhenZooming(true);
        
        //onceStartMyLocation();
showMyLocation();
mMapView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
app.mBMapMan.getLocationManager().removeUpdates(locationListener);
}
return false;
}
});
TextView t =(TextView)findViewById(R.id.username);
        Intent intent = getIntent();
        Bundle bundle=intent.getExtras();
        
        String vale = bundle.getString("abc");
        vale ="用户名 :"+vale;
        t.setText(vale);
        
        Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
        chronometer.setFormat("计时:%s");
        chronometer.start();

        // 初始化搜索模块,注册事件监听
        mSearch = new MKSearch();
        mSearch.init(app.mBMapMan, new MKSearchListener(){

public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
// TODO Auto-generated method stub
}

public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
// TODO Auto-generated method stub
}

public void onGetDrivingRouteResult(MKDrivingRouteResult res,int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(RoutePlan.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
   // 此处仅展示一个方案作为示例
   routeOverlay.setData(res.getPlan(0).getRoute(0));
   mMapView.getOverlays().clear();
   mMapView.getOverlays().add(routeOverlay);
   mMapView.invalidate();
   mMapView.getController().animateTo(res.getStart().pt);
}

public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}

public void onGetTransitRouteResult(MKTransitRouteResult res,
int error) {
Log.d("RoutePlan", "the res is " + res + "__" + error);
if (error != 0 || res == null) {
Toast.makeText(RoutePlan.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
return;
}
TransitOverlay  routeOverlay = new TransitOverlay (RoutePlan.this, mMapView);
   // 此处仅展示一个方案作为示例
   routeOverlay.setData(res.getPlan(0));
   mMapView.getOverlays().clear();
   mMapView.getOverlays().add(routeOverlay);
   mMapView.invalidate();
   
   mMapView.getController().animateTo(res.getStart().pt);
}

public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int error) {
if (error != 0 || res == null) {
Toast.makeText(RoutePlan.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
return;
}
RouteOverlay routeOverlay = new RouteOverlay(RoutePlan.this, mMapView);
   // 此处仅展示一个方案作为示例
   routeOverlay.setData(res.getPlan(0).getRoute(0));
   mMapView.getOverlays().clear();
   mMapView.getOverlays().add(routeOverlay);
   mMapView.invalidate();
   mMapView.getController().animateTo(res.getStart().pt);
}
        });
        
     // 设定搜索按钮的响应
        mBtnDrive = (Button)findViewById(R.id.drive);
        mBtnTransit = (Button)findViewById(R.id.transit);
        mBtnWalk = (Button)findViewById(R.id.walk);
        
        OnClickListener clickListener = new OnClickListener(){
public void onClick(View v) {
SearchButtonProcess(v);
}
        };
        
        mBtnDrive.setOnClickListener(clickListener); 
        mBtnTransit.setOnClickListener(clickListener); 
        mBtnWalk.setOnClickListener(clickListener);
}
void SearchButtonProcess(View v) {
// 处理搜索按钮响应
EditText editSt = (EditText)findViewById(R.id.start);
EditText editEn = (EditText)findViewById(R.id.end);
// 对起点终点的name进行赋值,也可以直接对坐标赋值,赋值坐标则将根据坐标进行搜索
MKPlanNode stNode = new MKPlanNode();
stNode.name = editSt.getText().toString();
MKPlanNode enNode = new MKPlanNode();
enNode.name = editEn.getText().toString();

// 实际使用中请对起点终点城市进行正确的设定
if (mBtnDrive.equals(v)) {
mSearch.drivingSearch("北京", stNode, "上海", enNode);
} else if (mBtnTransit.equals(v)) {
mSearch.transitSearch("北京", stNode, enNode);
} else if (mBtnWalk.equals(v)) {
mSearch.walkingSearch("北京", stNode, "北京", enNode);
}
}

// 一直定位自己的位置
private LocationListener locationListener = new LocationListener() {

public void onLocationChanged(Location location) {
if (location != null) {
Double geolat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
GeoPoint mypoint = new GeoPoint(geolat.intValue(), geoLng.intValue());
mMapView.getController().animateTo(mypoint);
if (overitem != null) {
mMapView.getOverlays().remove(overitem);
}
Drawable marker = getResources().getDrawable(R.drawable.iconmarka);
overitem = new OverlayT(marker, mypoint,RoutePlan.this);
mMapView.getOverlays().add(overitem);
}
}
};

private void showMyLocation() {
app.mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_GPS_PROVIDER);
app.mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_NETWORK_PROVIDER);
app.mBMapMan.getLocationManager().requestLocationUpdates(locationListener);
app.mBMapMan.getLocationManager().setNoitifyInternal(5, 2);
}

@Override
protected void onResume() {
app.mBMapMan.start();
super.onResume();
}
@Override
protected void onPause() {
app.mBMapMan.getLocationManager().removeUpdates(locationListener);
app.mBMapMan.stop();
super.onPause();
}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
// 返回键
private long exitTime = 0;
@SuppressWarnings("static-access")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == event.KEYCODE_BACK) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
举报
王小志
发帖于6年前 2回/2K+阅
顶部