Google服务地理定位问题

时间:2019-09-10 20:36:49

标签: android google-maps

问题是关于使用google service在地图上显示我当前的位置,我最初遵循了一个教程,并且效果很好,但是现在,它停止了,我没有任何结果(只有没有位置标记的空地图..图片如下)..是否可能由于api密钥问题?

enter image description here

public class MainActivity extends FragmentActivity implements public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
Location currentlocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;
Button btnLogout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    fetchlastlocation();
}

private void fetchlastlocation() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
        return;
    }
    Task<Location> task = fusedLocationProviderClient.getLastLocation();
    task.addOnSuccessListener(new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            if(location !=null){
                currentlocation=location;
                Toast.makeText(getApplicationContext(),currentlocation.getLatitude()+""+currentlocation.getLongitude(),Toast.LENGTH_SHORT).show();
                SupportMapFragment supportMapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.google_map);
                supportMapFragment.getMapAsync(MainActivity.this);
            }
        }
    });
}

@Override
public void onMapReady(GoogleMap googleMap) {
    LatLng latLng= new LatLng(currentlocation.getLatitude(),currentlocation.getLongitude());
    MarkerOptions markerOptions= new MarkerOptions().position(latLng).title("I'am Here");
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,15));
    googleMap.addMarker(markerOptions);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case REQUEST_CODE:
            if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                fetchlastlocation();
            }
            break;
    }
}
}

0 个答案:

没有答案