接着上面的项目《Android访问webservice.客户端登录注册》,实现餐厅搜索,这个在吃客游记里就做过了,然后把餐厅显示出来,可以把该餐厅加入轨迹
关于轨迹点操作的前后台实现和之前的登录注册差不多,这里主要说一下,用户查看自己的轨迹时候,在手机
端的显示。
1.从服务器把轨迹点拿下来
2.地图上显示点
3.把点连成线
看代码吧,注释比较详细,关键的画线操作就在OverItemT这个类中。
package seu.android007.xin;
import java.util.ArrayList;
import java.util.List;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import seu.android007.service.TraceServcie;
import seu.xin.entity.Trace;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;
public class MyTrace extends MapActivity {
static MapView mMapView = null;
LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove
MyLocationOverlay mLocationOverlay = null; // 定位图层
static View mPopView = null; // 点击mark时弹出的气泡View
TraceServcie traceServcie;
private ArrayList<Trace> myTraces = new ArrayList<Trace>();
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_trace);
traceServcie = new TraceServcie();
sp = this.getSharedPreferences("data", MODE_WORLD_READABLE);
int uid = sp.getInt("uid", 0);
myTraces = traceServcie.findTracesByUid(uid);
BMapApp app = (BMapApp) this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new BMapApp.MyGeneralListener());
}
app.mBMapMan.start();
// 如果使用地图SDK,请初始化地图Activity
super.initMapActivity(app.mBMapMan);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.setBuiltInZoomControls(true);
// 设置在缩放动画过程中也显示overlay,默认为不绘制
mMapView.setDrawOverlayWhenZooming(true);
// 添加定位图层
mLocationOverlay = new MyLocationOverlay(this, mMapView);
mMapView.getOverlays().add(mLocationOverlay);
// 注册定位事件
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint pt = new GeoPoint(
(int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapView.getController().animateTo(pt);
}
}
};
if (myTraces.size() == 0) {
System.out.println("轨迹为空,调转到MyActivityGroup");
AlertDialog.Builder builder = new AlertDialog.Builder(MyTrace.this);
builder.setTitle("提醒");
builder.setMessage("没有轨迹,请添加后再查看");
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MyTrace.this, MyActivityGroup.class);
startActivity(intent);
MyTrace.this.finish();
}
});
builder.create().show();
} else {
System.out.println("获得点后添加到图层");
// 添加ItemizedOverlay
Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要标在地图上的资源
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight()); // 为maker定义位置和边界
mMapView.getOverlays().add(new OverItemT(marker, this, myTraces)); // 添加ItemizedOverlay实例到mMapView