繁体   English   中英

我如何在android中获取当前位置的连续更新

[英]how can i get continuous updates of current location in android

我已经搜索了所有链接,但是随着位置的变化,我仍然无法从gps获取连续的数据。 我每次必须按下按钮才能获取位置。 我想要的是,该地图会不断通过单击1个按钮来更新位置,并每次返回纬度,经度,速度,距离和其他信息。

这是我的代码

public class Gps extends Activity {

 private EditText lat,lon,speed,acc,alt,t,add;
 private ProgressBar progress;
 private GoogleMap googleMap;
 private LocationManager locManager;
 private LocationListener locListener = new MyLocationListener();

 private boolean gps_enabled = false;
 private boolean network_enabled = false;

 public Looper mLooper;

 Geocoder geocoder;
 List<Address> addresses;


 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gps);

    lat = (EditText) findViewById(R.id.lat);
    lon = (EditText) findViewById(R.id.lon);
    acc = (EditText) findViewById(R.id.acc);
    speed = (EditText) findViewById(R.id.speed);
    alt = (EditText) findViewById(R.id.alt);
    t = (EditText) findViewById(R.id.time);
    add = (EditText) findViewById(R.id.add);
    progress = (ProgressBar) findViewById(R.id.p1);
    progress.setVisibility(View.GONE);

    //buttonGetLocation.setOnClickListener(this);

    locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        // check if map is created successfully or not
        if (googleMap == null) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                    .show();
        }
    }
    //googleMap.setMyLocationEnabled(true);

}
public void onClickb(View v) {
    // TODO Auto-generated method stub

    new find().run();
    //Connect c = new Connect();
    //c.execute();
}




 class MyLocationListener implements LocationListener
 {

    public void onLocationChanged(Location location) 
    {
        if (location != null)
        {

            // This needs to stop getting the location data and save the battery power.
            locManager.removeUpdates(this);


            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            double altitiude =location.getAltitude();
            double accuracy = location.getAccuracy();
            double time = location.getTime();
            String spmsg = null,s=null; 


            try
            {       
                GetCompleteAddressString ga = new GetCompleteAddressString();
                s = ga.getAddress(latitude, longitude); 
                //Toast.makeText(Gps.this,s, Toast.LENGTH_LONG).show();                 
                Float thespeed=location.getSpeed();
                Float sp=(thespeed/1000);
                spmsg="Speed : "+sp;
                //Toast.makeText(Gps.this,spmsg, Toast.LENGTH_LONG).show();                             
            }
            catch(Exception e)
            {
                Toast.makeText(Gps.this,e.toString(), Toast.LENGTH_LONG).show();
            }

            lat.setText("Longitude : "+longitude);
            lon.setText("Latitude : "+ latitude);
            acc.setText("Accuracy : "+ accuracy);
            t.setText("Time : "+ time);
            speed.setText(spmsg);
            alt.setText("Altitude : " +altitiude);
            add.setText(s);
            progress.setVisibility(View.GONE);

            CameraPosition cameraPosition = new CameraPosition.Builder().target(
                    new LatLng(latitude, longitude)).zoom(14).build();

            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");

            // adding marker
            googleMap.addMarker(marker);
           // Looper.getMainLooper().quit();
        }
        //mLooper.quit();
        //Looper.myLooper().quit();
}

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

    }

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

    }

    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }



}
public class find extends Thread
 {

    public find()
    {
        locListener = new MyLocationListener();

        try 
        {
            gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } 
        catch (Exception ex) 
        {}

        try 
        {
            network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        }
        catch (Exception ex)
        {}

        // don't start listeners if no provider is enabled
        if (!gps_enabled && !network_enabled)
        {
            Toast.makeText(getApplicationContext(), "Sorry, location is not determined. Please enable location providers", Toast.LENGTH_LONG).show();
            progress.setVisibility(View.GONE);

        }   

    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        //super.run();
        //Looper.prepare();
        /*if(Looper.myLooper() == null) { // check already Looper is associated or not.
               Looper.prepare(); // No Looper is defined So define a new one
            }
        */


        if (gps_enabled) {
            locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 0, locListener);
            }
            if (network_enabled) {
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5, 0, locListener);
            }
        //Looper.loop();
    }

 }

}

您正在使它在第一个位置之后停止收听。

public void onLocationChanged(Location location) 
{
    if (location != null)
    {
        // This needs to stop getting the location data and save the battery power.
        locManager.removeUpdates(this); <-- REMOVE THIS LINE

onOLocationChangeListener已经为您完成了。

onLocationChanged(Location location)

在知道新的用户位置时调用。

示例/源代码可以在这里找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM