我正試圖在3天內解決這個問題。我失去了調試的視野,無法看到我的錯誤。 我想構建一個請求當前位置的應用程序。爲什麼getLastLocation返回null?
我的getLastLocation返回null,是否有任何一點指向我在哪裏做錯了?
這裏是我的代碼
public class MapActivityFragment extends FragmentActivity implements
LocationListener,
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnMarkerDragListener,
GoogleMap.OnMapLongClickListener, View.OnClickListener {
//Our Map
private GoogleMap mMap;
//To store longitude and latitude from map
private double longitude;
private double latitude;
//Google ApiClient
private GoogleApiClient googleApiClient;
private ImageButton buttonCurrent;
private TextView tv;
private String myLocation;
@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);
tv = (TextView) findViewById(R.id.textViewTv);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//Initializing googleapi client
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
buttonCurrent = (ImageButton) findViewById(R.id.buttonCurrent);
buttonCurrent.setOnClickListener(this);
//Initializing views and adding onclick listeners
}
@Override
protected void onStart() {
googleApiClient.connect();
// Log.d("Tag Name", "GOOGLE : : : " + googleApiClient);
super.onStart();
}
@Override
protected void onResume() {
googleApiClient.connect();
super.onResume();
}
//Getting current location
private void getCurrentLocation() {
//mMap.clear();
//Creating a location object
Log.d("Tag Name", "GOOGLE : : : " + googleApiClient);
Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
Log.d("Tag Name", "asdsa " + location);
Toast.makeText(MapActivityFragment.this, "googleApiclient:" + googleApiClient, Toast.LENGTH_LONG).show();
Toast.makeText(MapActivityFragment.this, "location:" + location, Toast.LENGTH_LONG).show();
mMap.clear();
if (location != null) {
Log.d("Tag Name", "2222");
//Getting longitude and latitude
longitude = location.getLongitude();
latitude = location.getLatitude();
tv.setText("longitude: " + longitude + " " + "latitude: " + latitude);
//Toast.makeText(this, "long ; " + longitude, Toast.LENGTH_LONG).show();
Log.d("Tag Name", "Log Message");
//moving the map to location
moveMap();
}
}
//Function to move the map
private void moveMap() {
//String to display current latitude and longitude
String msg = latitude + " , " + longitude;
//Creating a LatLng Object to store Coordinates
LatLng latLng = new LatLng(latitude, longitude);
//Adding marker to map
mMap.addMarker(new MarkerOptions()
.position(latLng) //setting position
.draggable(true) //Making the marker draggable
.title("Current Location")); //Adding a title
//Moving the camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//Animating the camera
//mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
//Displaying current coordinates in toast
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng latLng = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.setOnMarkerDragListener(this);
mMap.setOnMapLongClickListener(this);
}
@Override
public void onConnected(Bundle bundle) {
Log.i("TAG", "Location services connected.");
getCurrentLocation();
}
@Override
public void onConnectionSuspended(int i) {
Log.i("TAG", "Location services suspended. Please reconnect.");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onMapLongClick(LatLng latLng) {
mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(latLng)
.draggable(true));
}
@Override
public void onMarkerDragStart(Marker marker) {
}
@Override
public void onMarkerDrag(Marker marker) {
}
@Override
public void onMarkerDragEnd(Marker marker) {
latitude = marker.getPosition().latitude;
longitude = marker.getPosition().longitude;
moveMap();
}
@Override
public void onClick(View v) {
if (v == buttonCurrent) {
mMap.setMyLocationEnabled(true);
getCurrentLocation();
//moveMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(40, 40)).title("Hello World"));
}
}
@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
tv.setText("Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
權限怎麼樣?在清單中添加了必需的權限(並且自API23以來在運行時請求)? – Opiatefuchs
和運行時權限檢查?你在API => 23上開發嗎? – Opiatefuchs