用于交通应用的android gps app

时间:2011-05-31 08:32:23

标签: android gps traffic

我目前已经完成了Android开发的基础知识,并且正在尝试学习在智能手机中为我的交通系统项目制作一个Android gps应用程序。做复杂的代码还不是很好。请告知一些资源或教程,这将极大地帮助我制作这个应用程序。     - 一旦接近特定的路段,该应用程序将每隔10秒通过短信发送gps位置(纬度和经度和时间)(可能像路长0.5公里)     - 如果电话通过该特定路段,它将停止发送其位置

2 个答案:

答案 0 :(得分:1)

为什么使用短信服务发送位置?尝试使用网络服务。

要实现此应用程序,您必须使用LocationManager和LocationListener库。 您可以从创建实现LocationListener的gps监听类开始,如下所示

public class CTLocationListener implements LocationListener{




@Override
public void onLocationChanged(Location location) {
    Log.w("LOCATION CHANGED", ""+location);



    if(location != null) {

        Constants.LATTITUDE = location.getLatitude();
        Constants.LONGITUDE = location.getLongitude();
        Constants.kAccuracy = location.getAccuracy();
        Constants.ALTITUDE_VALUE = location.getAltitude();
    }


}

@Override
public void onProviderDisabled(String provider) {
    Log.i("PROVIDER", "DISABLED:"+provider);


}

@Override
public void onProviderEnabled(String provider) {
    Log.i("PROVIDER", "ENABLED:"+provider);

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.i("EXTRAS", ""+extras);
    Log.i("Provider status", ""+status);
}

}

这里我将更新的位置存储在Constants文件中。启动一个定期检查位置的sheduler,如果它们符合您所需的位置,则开始发送消息。

要触发gps,您可以使用

LocationListener locationListener = new CTLocationListener(); 
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);

您需要的调度程序是一个时间任务。这可以实现为

public class showAccuracy extends TimerTask {

    @Override
    public void run() {

        ghandler.post(new Runnable() {

            @Override
            public void run() {
                if((Constants.Latitude == yourlatitude) && .....) {

}                 }             });         }

}

答案 1 :(得分:0)

public class HomeActivity extends Activity implements LocationListener{
public static Context mContext;
private double latitude, longitude;
 public LocationManager mLocManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    mContext=this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homelayout);


    mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            this);
    mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
            0, this);
    locationUpdate();
    ((Button) this.findViewById(R.id.ButtonHome))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {

                        startActivity(new Intent(HomeActivity.this,
                                DefaultDisplay.class));

                }
            });

    ((Button) this.findViewById(R.id.ButtonProfile))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                MyProfile.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","MyProfile");
                        startActivity(intent);
                    }
                }
            });

    ((Button) this.findViewById(R.id.ButtonNotifications))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                ShowAllNotificationActiviry.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","ShowAllNotificationActiviry");
                        startActivity(intent);
                    }
                }
            });

    ((Button) this.findViewById(R.id.ButtonFavorites))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                FavoritesActivity.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","FavoritesActivity");
                        startActivity(intent);
                    }
                }
            });

            ((Button) this.findViewById(R.id.ButtonMore))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                        startActivity(new Intent(HomeActivity.this,
                                MoreListActivity.class));
                }
            });

}

public void locationUpdate()
{
    CellLocation.requestLocationUpdate();
}


public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}



public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    GUIStatics.latitude=location.getLatitude();
    GUIStatics.longitude= location.getLongitude();
    Log.v("Test", "IGA" + "Lat" + latitude + "   Lng" + longitude);
    //mLocManager.r

    getAddress(latitude, longitude);
    if(location!=null)
    {

    mLocManager.removeUpdates(this);
    }
    // Toast.makeText(this, "Lat" + latitude + "   Lng" + longitude,
    // Toast.LENGTH_SHORT).show();
}


public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(HomeActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}


public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}


public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
     if(arg1 == 
            LocationProvider.TEMPORARILY_UNAVAILABLE) { 
                                    Toast.makeText(HomeActivity.this, 
            "LocationProvider.TEMPORARILY_UNAVAILABLE", 
            Toast.LENGTH_SHORT).show(); 
                        } 
                        else if(arg1== LocationProvider.OUT_OF_SERVICE) { 
                                    Toast.makeText(HomeActivity.this, 
            "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show(); 
                        } 

}

}

这是用于获取纬度和经度或设备的代码,并且还使用获取地址函数获取有关lat long的所有信息。

这里有一些其他的URL非常适合你。 http://developer.android.com/reference/android/location/LocationManager.html http://code.google.com/p/open-gpstracker/ http://androidcommunity.com/forums/f4/android-gps-support-and-info-55/ http://www.devx.com/wireless/Article/43005 http://android-er.blogspot.com/2011/02/get-locationlatitude-and-longitude-from.html

我希望这对你非常有用。