如何使用当前位置获取谷歌地图?

时间:2016-05-30 09:49:04

标签: android gps currentlocation

我尝试使用当前位置获取谷歌地图但我收到此错误

FATAL EXCEPTION: main java.lang.NullPointerException

map= ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

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));
            }
        };
    }
}

2 个答案:

答案 0 :(得分:0)

这可能是因为此时地图还没有准备就绪。

@Override
public void onMapReady(GoogleMap googleMap) {

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

答案 1 :(得分:0)

在回复您对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());
    }
}