0

我想从以下 Android 代码中获取纬度和经度。

location = locationManager.getLastKnownLocation(provider);

我来了error location is null

这是代码:

package com.p;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
 import android.widget.Toast;
 public class GooglemapActivity extends Activity implements LocationListener {
  private TextView latituteField;
        private TextView longitudeField; 
   private LocationManager locationManager;
    private String provider;


 /** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    } else {
        latituteField.setText("Provider not available");
        longitudeField.setText("Provider not available");
    }
}

/* Request updates at startup */
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
}

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

}

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

}

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

我添加了以下权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

但我收到以下错误:

提供者不可用

4

5 回答 5

1

您尚未定义有效的标准。定义如下

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);//true if required
    criteria.setBearingRequired(false);//true if required
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);//search for enabled provider
于 2012-05-02T12:20:12.800 回答
1

如果最后一个已知位置为空,那是因为手机没有最后一个已知位置。如果没有缓存位置,您需要通过网络或 GPS 请求更新的位置。请注意,此过程可能需要时间,因此必须异步完成。

你需要阅读这份文件:

http://developer.android.com/guide/topics/location/obtaining-user-location.html

我还建议阅读以下内容:

http://android-developers.blogspot.co.uk/2011/06/deep-dive-into-location.html

于 2012-05-02T12:03:53.237 回答
0

认为您需要指定要从哪个提供商接收更新。

你有两个选择:

  • GPS_PROVIDER
  • NETWORK_PROVIDER

你可能错过了这样的事情:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
于 2012-05-02T12:04:32.717 回答
0
  1. 您是否在设置 -> 位置中打开了 GPS?
  2. 你为什么使用(int) (location.getLatitude())-- 纬度/经度是float
于 2012-05-02T12:05:09.643 回答
0

我认为您可能还想添加以下权限,因为我很确定 FINE 依赖于 COARSE ...

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
于 2014-05-20T13:29:46.853 回答