【发布时间】:2017-03-28 23:36:07
【问题描述】:
我正在尝试在我当前的位置放置一个标记(使用 google maps api)。虽然 LatLng 中的直接输入值正常工作,但间接调用 currentLocation.getLatitude() 和 currentLocation.getLongitude() 由于某种原因使我的应用程序崩溃,我知道这是这一行,因为在我添加此行之前应用程序不会崩溃。
这是我的 MapsActivity 类-
package com.example.shreyass.tourist;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
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 com.google.android.gms.common.ConnectionResult;
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.internal.IGoogleMapDelegate;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.common.api.GoogleApiClient;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
private GoogleMap mMap;
private Location currentLocation;
GoogleApiClient googleApiClient;
@Override
protected void onStop() {
super.onStop();
googleApiClient.disconnect();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super`enter code here`.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
googleApiClient.connect();
// 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);
}
/**
* 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.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker at current location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if(ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)
!=PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MapsActivity.this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1);
}
else {
currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
【问题讨论】:
-
尝试输入一个 try catch 并找到你得到的异常
-
能否请您也发布您的 Logcat..
-
Shijil 的回答很有效。感谢您的时间! :)
标签: android google-api