2015-01-08 21 views
0

我已经搜索了所有的链接,我仍然无法从GPS获取连续的数据,因为我的位置发生变化。我必须每次按下按钮以获取位置。我想要的是,地图不断更新位置,并且每按一次按钮就会返回纬度,经度,速度,距离和其他信息。我怎样才能获得连续更新的当前位置在Android

这里是我的代码

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(); 
    } 

} 

} 
+0

您可以使用与您需要创建onOLocationChangeListener相同的 – Anjali

+2

服务。 我建议你看看这个: http://gabesechansoftware.com/location-tracking/ –

+0

使用Google的指南你将没有问题:https://developer.android.com/guide/topics/location /strategies.html – shkschneider

回答

4

你使它停止在第一位置之后听。

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 
0

onOLocationChangeListener有没有已经这样做了你。

onLocationChanged(Location location) 

当新的用户位置已知时调用。

例/源代码可以发现here