【问题标题】:Latitude and Longitude not getting in lollipop version using google play services使用谷歌播放服务没有进入棒棒糖版本的纬度和经度
【发布时间】:2016-03-04 10:18:26
【问题描述】:

我尝试了一个使用 Google Play 服务显示纬度和经度的应用程序,它在以下 Lollipop 版本中运行良好。但它在 Lollipop 上显示“未检测到位置”。我使用了以下代码:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient mGoogleApiClient;
private Location mLocation;
private static final String TAG = "MainActivity";
private TextView mLatitudeTextView;
private TextView mLongitudeTextView;
Geocoder geocoder;
List<Address> addresses;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mLatitudeTextView = (TextView) findViewById((R.id.latitude_textview));
    mLongitudeTextView = (TextView) findViewById((R.id.longitude_textview));
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

}

@Override
public void onConnected(Bundle bundle) {

    mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLocation != null) {
   mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
          mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));

    } else {
        Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onConnectionSuspended(int i) {

    Log.i(TAG, "Connection Suspended");
    mGoogleApiClient.connect();
}


@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

    Log.i(TAG, "Connection failed. Error: " + connectionResult.getErrorCode());
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
}

我在 gradle 中包含了 compile com.google.android.gms:play-services:8.1.0,在清单文件中包含了 android.permission.ACCESS_COARSE_LOCATION

请任何人帮我解决这个问题。

【问题讨论】:

    标签: android google-play-services android-5.0-lollipop android-location


    【解决方案1】:

    尝试添加&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
    如果不尝试使用本教程:http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

    【讨论】:

    • 确保清单文件中正确包含所有内容。
    • 我试过这个教程sitepoint.com/…。这表示 Fine Location 使用设备 GPS、蜂窝数据和 WiFi 来获得最准确的位置,但它会消耗电池寿命。粗略定位使用设备蜂窝数据和 WiFi 来获取位置。它不会像 Fine 一样准确,但使用的电池电量要少得多,返回的位置精度相当于城市街区。
    【解决方案2】:

    使用此方法连接 Google API Client:

    GoogleApiClient apiClient=null;
    LocationRequest mLocationRequest=null;
    
    private void setLocationLocationRequest() {
    
            try {
                apiClient=new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
    
                mLocationRequest = new LocationRequest();
                mLocationRequest.setInterval(29000);
                mLocationRequest.setFastestInterval(5000);
                mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                apiClient.connect();
    
            }catch (Exception e){
                Log.d("LS", e.getMessage() == null ? "" : e.getMessage());
            }
    
        }
    

    更多详情,请查看this Answerthis answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      相关资源
      最近更新 更多