我怎样才能在 android 中获得当前位置的持续更新
how can i get continuous updates of current location in android
我已经搜索了所有链接,但随着位置的变化,我仍然无法从 gps 获取连续数据。我每次都必须按下按钮才能获取位置。我想要的是,该地图每次单击 1 个按钮都会不断更新位置,并且 returns 纬度、经度、速度、距离和其他信息。
这是我的代码
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();
}
}
}
onOLocationChangeListener
是否已经为您完成了此操作。
onLocationChanged(Location location)
在已知新用户位置时调用。
Example/Source代码可以找到here
您导致它在第一个位置后停止收听。
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
我已经搜索了所有链接,但随着位置的变化,我仍然无法从 gps 获取连续数据。我每次都必须按下按钮才能获取位置。我想要的是,该地图每次单击 1 个按钮都会不断更新位置,并且 returns 纬度、经度、速度、距离和其他信息。
这是我的代码
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();
}
}
}
onOLocationChangeListener
是否已经为您完成了此操作。
onLocationChanged(Location location)
在已知新用户位置时调用。
Example/Source代码可以找到here
您导致它在第一个位置后停止收听。
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