Search code examples
androidgoogle-mapsandroid-fragmentsgoogle-maps-android-api-2supportmapfragment

Google Maps API Android show Map of address in MapFragment


I have a SupportMapFragment

private GoogleMap mMap;
// ...
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

I want to load the map for a particular address. The above code, I got at https://developers.google.com/maps/documentation/android/map. I also saw this post Google maps API in Android which talks about starting an activity. I don't want to do that but load it in the fragment. How do I do this? The APIs of Google Map (http://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html) also don't have any appropriate method for loading map with specific address.


Solution

  • Take a look at this code,

        public class MainActivity extends Activity {
      static final LatLng HAMBURG = new LatLng(53.558, 9.927);
      static final LatLng KIEL = new LatLng(53.551, 9.993);
      private GoogleMap map;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    
        if (map!=null){
          Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
              .title("Hamburg"));
          Marker kiel = map.addMarker(new MarkerOptions()
              .position(KIEL)
              .title("Kiel")
              .snippet("Kiel is cool")
              .icon(BitmapDescriptorFactory
                  .fromResource(R.drawable.ic_launcher)));
        }
    
      }