创建Android GPS跟踪应用程序 [英] Create an Android GPS tracking application

查看:231
本文介绍了创建Android GPS跟踪应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我已经把Android开发作为一个兴趣,并且正在寻找开发一个可以使用Google Maps查找和跟踪用户位置的应用程序。



一旦应用程序具有GPS锁定,应用程序可以通过使用覆盖类绘制路线来跟踪其移动。



我看到类似的应用程序,如Mytracks是开源的,但他们对我来说太复杂了。



理想情况下,我很想创建一个这样的应用程序。





他是我的下面的代码,没有导入。 / p>

我想要做的是创建一个数组的地理点。
每次位置更改时,都会创建一个新的地理点。
然后我尝试使用for循环遍历每个地理点,并在它们之间绘制一条路径。

  public class跟踪扩展MapActivity实现LocationListener {

LocationManager locman;
LocationListener loclis;
位置位置;
私人MapView地图;

列表< GeoPoint> geoPointsArray = new ArrayList< GeoPoint>();
私有MapController控制器;
String provider = LocationManager.GPS_PROVIDER;
双纬;
双龙;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initMapView();
initMyLocation();
locman =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
// locman.requestLocationUpdates(provider,60000,100,loclis);
// Location = locman.getLastKnownLocation(provider);

}

/ **查找并初始化地图视图。 * /
private void initMapView(){
map =(MapView)findViewById(R.id.map);
controller = map.getController();
map.setSatellite(false);
map.setBuiltInZoomControls(true);
}

/ **查找地图上的当前位置。 * /
private void initMyLocation(){
final MyLocationOverlay overlay = new MyLocationOverlay(this,map);
overlay.enableMyLocation();
overlay.enableCompass(); //在模拟器中不起作用
overlay.runOnFirstFix(new Runnable(){
public void run(){
//放大到当前位置
controller.setZoom(24 );
controller.animateTo(overlay.getMyLocation());
}
});
map.getOverlays()。add(overlay);
}

@Override
public void onLocationChanged(Location location){
if(Location!= null){
lat = Location.getLatitude() ;
lon = Location.getLongitude();
GeoPoint New_geopoint = new GeoPoint((int)(lat * 1e6),
(int)(lon * 1e6));
controller.animateTo(New_geopoint);

}

}

class MyOverlay extends Overlay {
public MyOverlay(){
}

public void draw(Canvas canvas,MapView mapv,boolean shadow){
super.draw(canvas,mapv,shadow);

投影投影= map.getProjection();
路径p = new Path(); $($ i $)$ {
if(i == geoPointsArray.size() - 1){
break;
for(int i = 0; i< geoPointsArray.size(); i ++)
}
Point from = new Point();
指向= new Point();
projection.toPixels(geoPointsArray.get(i),from);
projection.toPixels(geoPointsArray.get(i + 1),to);
p.moveTo(from.x,from.y);
p.lineTo(to.x,to.y);
}
绘制mPaint = new Paint();
mPaint.setStyle(Style.STROKE);
mPaint.setColor(0xFFFF0000);
mPaint.setAntiAlias(true);
canvas.drawPath(p,mPaint);
super.draw(画布,地图,阴影);
}
}

@Override
public void onProviderDisabled(String provider){
// TODO自动生成的方法存根



@Override
public void onProviderEnabled(String provider){
// TODO自动生成的方法存根

}

@Override
public void onStatusChanged(String provider,int status,Bundle extras){
// TODO自动生成的方法存根

}

@Override
protected boolean isRouteDisplayed(){
// TODO自动生成的方法stub
return false;
}
}

应用程序运行正常,没有错误,但没有路径绘制,位置点只是移动我移动。



任何帮助将非常感谢
谢谢。

解决方案

您感兴趣的Android移动应用程序 open-gpstracker 的源代码可用这里



您可以使用SVN客户端应用程序或通过Git检查代码:





调试源代码一定会帮助您。


Recently I've taken up android development as a hobby and was looking to develop an application that can find and track a users position using Google Maps.

Once the application has a GPS lock, The application can track their movements by drawing a route using an overlay class.

I've seen similar applications like Mytracks that are open source but they're too complex for me right now.

Ideally i'd love to create an application that looks like this

He is my code below without the imports.

What I'm trying to do is create an array of geopoints. Every time the location changes a new geopoint is created. I then try to use a for loop to iterate through each geopoint and draw a path between them.

public class Tracking extends MapActivity implements LocationListener {

LocationManager locman;
LocationListener loclis;
Location Location;
private MapView map;

List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
private MapController controller;
String provider = LocationManager.GPS_PROVIDER;
double lat;
double lon;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);
    initMapView();
    initMyLocation();
    locman = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // locman.requestLocationUpdates(provider,60000, 100,loclis);
    // Location = locman.getLastKnownLocation(provider);

}

/** Find and initialize the map view. */
private void initMapView() {
    map = (MapView) findViewById(R.id.map);
    controller = map.getController();
    map.setSatellite(false);
    map.setBuiltInZoomControls(true);
}

/** Find Current Position on Map. */
private void initMyLocation() {
    final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
    overlay.enableMyLocation();
    overlay.enableCompass(); // does not work in emulator
    overlay.runOnFirstFix(new Runnable() {
        public void run() {
            // Zoom in to current location
            controller.setZoom(24);
            controller.animateTo(overlay.getMyLocation());
        }
    });
    map.getOverlays().add(overlay);
}

@Override
public void onLocationChanged(Location location) {
    if (Location != null) {
        lat = Location.getLatitude();
        lon = Location.getLongitude();
        GeoPoint New_geopoint = new GeoPoint((int) (lat * 1e6),
                (int) (lon * 1e6));
        controller.animateTo(New_geopoint);

    }

}

class MyOverlay extends Overlay {
    public MyOverlay() {
    }

    public void draw(Canvas canvas, MapView mapv, boolean shadow) {
        super.draw(canvas, mapv, shadow);

        Projection projection = map.getProjection();
        Path p = new Path();
        for (int i = 0; i < geoPointsArray.size(); i++) {
            if (i == geoPointsArray.size() - 1) {
                break;
            }
            Point from = new Point();
            Point to = new Point();
            projection.toPixels(geoPointsArray.get(i), from);
            projection.toPixels(geoPointsArray.get(i + 1), to);
            p.moveTo(from.x, from.y);
            p.lineTo(to.x, to.y);
        }
        Paint mPaint = new Paint();
        mPaint.setStyle(Style.STROKE);
        mPaint.setColor(0xFFFF0000);
        mPaint.setAntiAlias(true);
        canvas.drawPath(p, mPaint);
        super.draw(canvas, map, shadow);
    }
}

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

}

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

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}
}

The application runs fine without errors but there is no path drawn, the location dot just moves as i move.

Any help would be greatly appreciated Thanks.

解决方案

The source code for the Android mobile application open-gpstracker which you appreciated is available here.

You can checkout the code using SVN client application or via Git:

Debugging the source code will surely help you.

这篇关于创建Android GPS跟踪应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆