javaandroidgoogle-mapsmapfragment

Google Maps and MapFragment and getMapAsync


how's it going?

I'm working on an android app that is based on Google maps and location services. I know a little bit about Google Maps, which I have learned on this project, but there are several things about GoogleMaps I dont know...

In my main activity, I use a mapFragment. I have my onMapReady method....

@Override
public void onMapReady(GoogleMap map) {

    double myCurrentLatitude = getGPS()[0];
    double myCurrentLongitude = getGPS()[1];

    CameraPosition myCurrentLocation = CameraPosition.builder()
            .target(new LatLng(myCurrentLatitude, myCurrentLongitude))
            .zoom(12)
            .bearing(0)
            .tilt(2)
            .build();

   map.moveCamera(CameraUpdateFactory.newCameraPosition(myCurrentLocation));
    map.getUiSettings().setScrollGesturesEnabled(true);         

}

This is just one method in the activity, and it is working fine, and when the activity is launched, it zooms in on where the user is.

Now, I want to be able to zoom the map to a particular location. Let's say the user wants to go somewhere, and I can get the latitude and longitude of where they want to go (doubles)....I have not been able to get this activity to change the focus of the map and zoom to this NEW location.

I have tried several things, but I just dont know how to manipulate a mapFragment.

My little humble research has shown me getMapAsync, but it seems like these are methods that are called when the activity is just launched. I am looking to write a method (I called it placeLocation(lat, long)... that can be called 3 minutes later/whenever I want and it will take in the lat/long I give it, and zoom my mapFragment on that location.

I tried this line in my placeLocation method

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

But android studio is being real bitter about this getMap() method - basically not even recognizing it. and basically I dont know what way to manipulate my map between getMap, getMapAsync...can you help?

Give me some pointers please?

Thanks for reading, and thanks for your time.

Your humble brother in code.


Solution

  • First you need to create a class variable, something like:

    private GoogleMap mMap;

    Now when your onMapReady() method fires your can grab the GoogleMap object and set it the class variable.

    @Override
    public void onMapReady(GoogleMap map) {
        mMap = map;
    
        double myCurrentLatitude = getGPS()[0];
        double myCurrentLongitude = getGPS()[1];
    
        CameraPosition myCurrentLocation = CameraPosition.builder()
                .target(new LatLng(myCurrentLatitude, myCurrentLongitude))
                .zoom(12)
                .bearing(0)
                .tilt(2)
                .build();
    
       mMap.moveCamera(CameraUpdateFactory.newCameraPosition(myCurrentLocation));
       mMap.getUiSettings().setScrollGesturesEnabled(true);  
    }
    

    Now when ever you need to make changes to the map just use the class variable "mMap" like this:

    private void mapMoveTo(double lat, double lng){
        LatLng latLng = new LatLng(lat, lng);
        if(mMap != null){
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomFactor));
        }
    }