问题描述
我的工作样本application.In这个应用程序,我想获得更新的位置的经度和纬度当用户与Android手机上的way.I移动已经实现定位管理类,如下所示:
i am working on sample application.In this application i would like to get updated location latitude and longitude when a user moving with android mobile on a way.I have implemented Location Manager class as follows :
private LocationManager locationManager;
private LocationListener locationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
locationListener);
}
private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
}
}
}
如何获得更新从回地面位置的经度和纬度?
How to get updated location latitude and longitude from back ground ?
请任何机构可以帮助我。
please any body help me.
推荐答案
如果您打算让纬度,虽然您的应用程序没有运行经度,你可以使用获得的纬度和经度后台服务,做任何任务要。而且也不要忘了在不需要的时候删除更新,因为获取更新是非常昂贵的操作设备的电池使用条款。
If you are intending to get the latitude and longitude while your application is not running, you can use a Service which get the latitude and longitude in background and do whatever task you want to. And also don't forget to remove updates when not needed because getting updates is very costly operation in terms of device battery use.
还有一件事,你并不需要检查的位置空值,因为onLocationChanged()将被调用仅在位置由您的供应商获得。
One more thing, you don't need to check the location for null value because onLocationChanged() will be called only when a location is got by the your provider.
虽然我也是刚接触Android系统。这可能会帮助你。你必须看到android的文档了这些服务类的方法实际上做的,当他们被称为。这是没有完成code。你必须实现LocationListener的这个自己。这只是一个例子程序,它显示了一个普通类:
Although I am also new to android. This may help you. You must see the android documentation what these Service class methods actually do and when they are called.This is not completed code. You have implement the locationlistener in this yourself. This is just an example program which shows a normal class:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class BackService extends Service {
private MyTimerTask mTimerTask;
private Timer mTimer;
@Override
public void onCreate() {
mTimer = new Timer();
mTimerTask = new MyTimerTask();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mTimer.schedule(mTimerTask, 0, 500);
Log.d("onStartCommand","onStartCommand called...");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
Log.i("BackService","BackService is running...");
doSomethingWithLocation();
}
}
}
这篇关于如何从回地面服务的位置的经度和纬度和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!