【发布时间】:2016-10-25 11:03:46
【问题描述】:
使用最新版本的 Android...您应该在使用他们的位置信息之前检查用户是否已授予您权限。我已经浏览了 android 文档,这是我想出的代码。
我正在检查用户是否已授予权限,如果他们还没有...然后我们询问,然后在结果上有一个回调函数等等。
我已经多次单步执行代码,一切似乎都运行良好,除了:当我最终尝试使用 fusedLocationApi.getLastLocation 以编程方式获取用户的位置时......它返回 NULL!!
我已经重新上传了我拥有的代码。
任何能解决这个问题并且它确实对我有用的人,你可以获得我所有的代表分数......
package com.example.greg.rightnow;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.Manifest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.plus.Plus;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks {
private GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;
private Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
// Create an instance of GoogleAPIClient.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addApi(Plus.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
// .addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
* Manifest.permission.ACCESS_FINE_LOCATION
*/
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == grantResults[0]) {
mMap.setMyLocationEnabled(true);
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LatLng myLat = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLat));
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== 0) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
else if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) ==-1) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LatLng myLat = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myLat));
}
}
@Override
public void onConnectionSuspended(int i) {
}
}
你会注意到我不得不注释掉这一行:
// .addOnConnectionFailedListener(this)
如果我取消对此的注释...“this”带有红色下划线,我收到一条错误消息“... in Builder cannot be applied to com.example.greg.MapsActivity etc”
【问题讨论】:
-
好吧,'this'(即您的 MapsActivity 类)没有实现 GoogleApiClient.OnConnectionFailedListener,这是您应该传递给
addOnConnectionFailedListener()developers.google.com/android/reference/com/google/android/gms/… 的类型 -
任何答案有用吗?您需要更多帮助吗?
-
当您找到答案时,不要修改问题,而是添加一个新答案并将其标记为已接受!通过这种方式,它仍然可以为其他人重复使用......
标签: android google-maps permissions