无法使 Android 位置 API 工作
Unable to get the Android Location API to work
我的目标是获取某人的当前位置并在 Google 地图上的那个位置放置一个标记。该位置似乎正在返回 null。当我调试时,似乎 OnConnection 回调的方法也不起作用。该设备已连接并且运行良好,因为我可以使用允许查找位置但不提供位置数据的 mMap.setMyLocationEnabled。我 运行 使用 HTC One M8。
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LatLng mLastLocationLatLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mMap.addMarker(new MarkerOptions().position(latLng).title("Test"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10), 500, null);
}
});
// Zoom in, animating the camera.
//mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public void onConnected(Bundle bundle) {
//Gets last location of user - is usually the current
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLastLocationLatLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLastLocationLatLng, 15));
}
else {
LatLng latLng = new LatLng(0,0);
Toast.makeText(this, "Location not Found", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
您没有在任何地方打电话给 googleApiClient.connect();
。这就是 none 的连接相关回调被触发的原因。
我的目标是获取某人的当前位置并在 Google 地图上的那个位置放置一个标记。该位置似乎正在返回 null。当我调试时,似乎 OnConnection 回调的方法也不起作用。该设备已连接并且运行良好,因为我可以使用允许查找位置但不提供位置数据的 mMap.setMyLocationEnabled。我 运行 使用 HTC One M8。
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private LatLng mLastLocationLatLng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
mMap.addMarker(new MarkerOptions().position(latLng).title("Test"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10), 500, null);
}
});
// Zoom in, animating the camera.
//mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@Override
public void onConnected(Bundle bundle) {
//Gets last location of user - is usually the current
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
mLastLocationLatLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLastLocationLatLng, 15));
}
else {
LatLng latLng = new LatLng(0,0);
Toast.makeText(this, "Location not Found", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
您没有在任何地方打电话给 googleApiClient.connect();
。这就是 none 的连接相关回调被触发的原因。