发现地点:谷歌播放位置服务或Android平台定位的API [英] Finding Location : Google Play Location Service or Android platform location APIs

查看:116
本文介绍了发现地点:谷歌播放位置服务或Android平台定位的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得用户的位置,我的新的导航应用程序。 我要经常检查用户的位置,它是准确的。 我用下面的code,从样品得到的位置。

i am trying to get the users location for my new navigation app. i want to check the users location frequently and it has to be accurate . I use the following code from sample to get the location .

public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
private String provider;
private TextView a;
private TextView b;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a = (TextView) findViewById(R.id.a);
    b = (TextView) findViewById(R.id.b);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
    } else {
        a.setText("Location not available");
        b.setText("Location not available");
    }
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider,0, 1, this);
}

@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    Double lat =  location.getLatitude();
    Double lng =  location.getLongitude();
    a.setText(String.valueOf(lat));
    b.setText(String.valueOf(lng));
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}
}

但位置cordinates没有更新,因为我动,我得到一个恒定的值总是... 有没有更好的方式来获得位置?据到Android文档,我可以使用谷歌播放位置服务或Android平台定位的API这一点。 我检查了样品code为它实现了LocationListener的接口获取用户位置。那么什么它们之间的实际差异?

But location cordinates are not updating as i move,i get a constant value always ... is there any better way to get location ? According to to android docs i can use Google Play Location Service or Android platform location APIs for this . I checked the sample code for both and it implements the LocationListener interface to get the user location .So whats the actual difference between them?

推荐答案

绝对使用新的谷歌播放位置服务。他们的工作比原来的位置,经理要好得多室内。由两个谷歌的开发者,该项目本优秀的视频显示多少户内准确性与新定位服务的改进:

Absolutely use the new google play location services. They work much better indoors than the old location manager. This excellent video by the two google developers working on the project show how much indoor accuracy is improved with the new location services:

<一个href="https://developers.google.com/events/io/sessions/325337477">https://developers.google.com/events/io/sessions/325337477

我已经创建了一个使用新的谷歌播放位置服务和工作在周期性背景的样本。检查出来:

I have created a sample that uses the new google play location services and works in the background periodically. Check it out:

<一个href="https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android">https://github.com/nickfox/GpsTracker/tree/master/phoneClients/android

这篇关于发现地点:谷歌播放位置服务或Android平台定位的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆