小编Faz*_*gal的帖子

查找位置:Google Play位置服务或Android平台位置API

我正在尝试为我的新导航应用程序获取用户位置.我想经常检查用户位置,它必须准确.我使用示例中的以下代码来获取位置.

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");
    }
} …
Run Code Online (Sandbox Code Playgroud)

gps android location geolocation

10
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1

geolocation ×1

gps ×1

location ×1