Android 如何获取当前位置的谷歌地图?

Android 如何获取当前位置的谷歌地图?,android,gps,currentlocation,Android,Gps,Currentlocation,我试图获取当前位置的谷歌地图,但出现了此错误 FATAL EXCEPTION: main java.lang.NullPointerException 在 java代码 public class MapFragment extends Fragment{ private TextView locationText; private TextView addressText; private GoogleMap map; public View onCreat

我试图获取当前位置的谷歌地图,但出现了此错误

FATAL EXCEPTION: main java.lang.NullPointerException

java代码

public class MapFragment extends Fragment{

    private TextView locationText;
    private TextView addressText;
    private GoogleMap map;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootview = inflater.inflate(R.layout.fragment_map, container, false);

        locationText = (TextView) rootview.findViewById(R.id.location);
        addressText = (TextView) rootview.findViewById(R.id.address);

        //replace GOOGLE MAP fragment in this Activity

        return rootview;
    }

    public void onMapReady(GoogleMap map) {

//make the call here. it will be called once the map is ready
        replaceMapFragment();
    }
    private void replaceMapFragment() {
        map= ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

        // Enable Zoom
        map.getUiSettings().setZoomGesturesEnabled(true);

        //set Map TYPE
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //enable Current location Button
        map.setMyLocationEnabled(true);

        //set "listener" for changing my location
        map.setOnMyLocationChangeListener(myLocationChangeListener());
    }

    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener() {
        return new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
                double longitude = location.getLongitude();
                double latitude = location.getLatitude();

                Marker marker;
                marker = map.addMarker(new MarkerOptions().position(loc));
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
                locationText.setText("You are at [" + longitude + " ; " + latitude + " ]");

                //get current address by invoke an AsyncTask object
                //new GetAddressTask(getActivity()).execute(String.valueOf(latitude), String.valueOf(longitude));
            }
        };
    }
}

这可能是因为地图目前还没有准备好

@Override
public void onMapReady(GoogleMap googleMap) {

//make the call here. it will be called once the map is ready
replaceMapFragment();
}

这可能是因为地图目前还没有准备好

@Override
public void onMapReady(GoogleMap googleMap) {

//make the call here. it will be called once the map is ready
replaceMapFragment();
}

针对您对Kasun答案的评论,要在地图上显示当前位置标记,请拨打:

googleMap.setMyLocationEnabled(true);
编辑

import com.google.android.gms.maps.SupportMapFragment;

public class MapFragment extends SupportMapFragment
      implements OnMapReadyCallback {

    @Override
    public void onCreate(Bundle inState) {
        super.onCreate(inState);

        // start a task for connecting to the map
        // onMapReady() will be called when it is complete
        getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap

        // Enable Zoom
        map.getUiSettings().setZoomGesturesEnabled(true);

        //set Map TYPE
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //enable Current location Button
        map.setMyLocationEnabled(true);

        //set "listener" for changing my location
        map.setOnMyLocationChangeListener(myLocationChangeListener());
    }
}

针对您对Kasun答案的评论,要在地图上显示当前位置标记,请拨打:

googleMap.setMyLocationEnabled(true);
编辑

import com.google.android.gms.maps.SupportMapFragment;

public class MapFragment extends SupportMapFragment
      implements OnMapReadyCallback {

    @Override
    public void onCreate(Bundle inState) {
        super.onCreate(inState);

        // start a task for connecting to the map
        // onMapReady() will be called when it is complete
        getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap

        // Enable Zoom
        map.getUiSettings().setZoomGesturesEnabled(true);

        //set Map TYPE
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        //enable Current location Button
        map.setMyLocationEnabled(true);

        //set "listener" for changing my location
        map.setOnMyLocationChangeListener(myLocationChangeListener());
    }
}

这是同样的问题。标记仍然隐藏着。我的代码正确吗?为了确保调用onMapReady(),您需要以不同的方式声明该类,请参阅我编辑的答案。很抱歉之前发布了不完整的答案。这是同样的问题。标记仍然隐藏着。我的代码正确吗?为了确保调用onMapReady(),您需要以不同的方式声明该类,请参阅我编辑的答案。抱歉之前发布了不完整的答案。