20

android 怎么实现实时GPS监控

我在android studio中模拟了一个Java服务器,连续发送经纬度数据。客户端这边接收数据后要怎么实现在地图上实时显示经纬度确立的点?(java服务器发送数据,建了一个Service接收数据,通过广播发送到Activity,Activity显示地图,同时接收广播数据,在onRecieve方法中showOverlay)在真机测试时,新建了Activity,通过一个Button启动Service,数据传递正常,通过另一个Button启动Activitiy,程序闪退

经纬度数据我是按GPGGA的格式写的,SDK用的是supermap iclient for android,自己试过在地图上标注指定点,坐标转换应该没关系

这个是onRecieve里的代码
``` Bundle bundle=intent.getExtras();
X2 = bundle.getDouble("x");
Y2 = bundle.getDouble("y");
mapView.getController().setCenter(new Point2D(X2,Y2));
point2D2.x=X2;
point2D2.y=Y2;
showOverlay();

地图的话是supermap Iserver发布的网址URL


```  private static final String DEFAULT_URL="http://192.168.1.12:8090/iserver/services/map-ElecChart5/rest/maps/CN484217_22000";
    protected MapView mapView;

        mapView=(MapView)this.findViewById(R.id.mapview)

        LayerView layerView=new LayerView(this);
        layerView.setURL(DEFAULT_URL);
        mapView.setBuiltInZoomControls(true);
        mapView.addLayer(layerView);

这个是闪退的Activity代码
```public class GpsActivity extends AppCompatActivity{
private static final String DEFAULT_URL="http://192.168.1.12:8090/iserver/services/map-ElecChart5/rest/maps/CN484217_22000";
MapView mapView;
private Button buttonEnd;

MyReceiver receiver = null;
private Button buttonStart;
double X2;
double Y2;
Point2D point2D2 = new Point2D(X2, Y2);

@Override
public void onCreate( Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);
    buttonStart = (Button) findViewById(R.id.button_start);
    buttonEnd = (Button) findViewById(R.id.button_stop);
    buttonEnd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       stopService(new Intent(GpsShowActivity.this, GpsService.class));
            finish();
        }
    });




    buttonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(new Intent(GpsActivity.this, GpsService.class));

        }
    });



    receiver=new MyReceiver();
    IntentFilter filter=new IntentFilter();
    filter.addAction("com.example.badius.iservertest.GpsService");
    GpsActivity.this.registerReceiver(receiver,filter);
}
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle=intent.getExtras();

        X2 = bundle.getDouble("x");
        Y2 = bundle.getDouble("y");
        mapView.getController().setCenter(new Point2D(X2,Y2));
        point2D2.x=X2;
        point2D2.y=Y2;
        showOverlay();

    }
}
 private void showOverlay(){
    DefaultItemizedOverlay overlay=new DefaultItemizedOverlay(getResources().getDrawable(R.mipmap.point_marker));
    OverlayItem overlayItem1=new OverlayItem(point2D2,"A","A");
    overlay.addItem(overlayItem1);

    mapView.getOverlays().add(new CustomOverlay());
    mapView.getOverlays().add(overlay);
    mapView.invalidate();
}

class CustomOverlay extends Overlay {

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow){
        super.draw(canvas,mapView,shadow);
        Paint paint=new Paint();
        Point point=mapView.getProjection().toPixels(new Point2D(X2,Y2),null);
        paint.setTextSize(24);
        paint.setStrokeWidth(0.8f);
        paint.setARGB(255,255,0,0);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawText("当前位置",point.x,point.y,paint);
    }
}

}

我是门外汉一个,还有什么描述不清楚的麻烦再提示,谢谢!



查看全部
qq_38131731
qq_38131731
2017/05/11 08:54
  • 数据
  • android-studio
  • 地图
  • android
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复