当我尝试获取用户设备位置时Google Map崩溃

时间:2019-07-30 15:40:18

标签: android google-maps

因此,我尝试按照以下指南将设备位置显示在我的地图上:googlemaps doc

我将代码放入活动中,并且能够正确设置所有代码。但是当我去运行项目时,我的地图已经崩溃了。我认为我的问题在于将代码放在onMapReady函数中的位置。我也不确定是否需要做一些特殊的事情,例如“ if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)!= Package ...”

任何建议都会很棒!

package com.example.myapplication;

import androidx.fragment.app.FragmentActivity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.location.Location;

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.location.FusedLocationProviderClient;
import com.google.android.gms.tasks.OnSuccessListener;


public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


    private GoogleMap mMap;
    private FusedLocationProviderClient fusedLocationClient;


    @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);
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(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(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));



        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        }
        LatLng temp = new LatLng(fusedLocationClient.getLastLocation().getResult().getLatitude()
                , fusedLocationClient.getLastLocation().getResult().getLongitude());

        mMap.addMarker(new MarkerOptions().position(temp).title("Marker in Home"));

    }
}

1 个答案:

答案 0 :(得分:0)

   if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
       //This means that you do not have permission to access the person's location information, so, you need to request the permissions

   } else {

        LatLng temp = new LatLng(fusedLocationClient.getLastLocation().getResult().getLatitude()
            , fusedLocationClient.getLastLocation().getResult().getLongitude());

        mMap.addMarker(new MarkerOptions().position(temp).title("Marker in Home"));

   }