Search code examples
androidapigoogle-mapsbuild-target

Adding Google maps api v2 to an existing project


I'm developing an Android App to incorporate the google maps api in a separate activity. But when I read tutorials it states the build target must be set to google api at project creation. My question is it possible to add the map to an existing project?


Solution

  • The device must have Google play services installed for Google Maps Android v2 to run:

    The API is now distributed as part of the Google Play services SDK, which you can download with the Android SDK Manager. To learn how to install the package, see Installing the Maps API SDK.

    You will find these docs useful!

    If your application is running v1, it's probably best to run a check to see if Google Play services is installed, and if not use the old map. I've not tested it, but check the answer here for running that check. I've also found, from here you can do this:

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext();
    if(status == ConnectionResult.SUCCESS) {
        //Success! Do what you want
    }
    

    And use the following types to determine if Google Play Services is installed on the device:

    public static int isGooglePlayServicesAvailable (Context context)

    Verifies that Google Play services is installed and enabled on this device, and that the version installed on this device is no older than the one required by this client.

    Returns status code indicating whether there was an error. Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.

    To add the map using a fragment you will need to do something like this:

    private GoogleMap map;
    private MapFragment mapFragment;
    private void InitMap()
    {
        mapFragment = ((MapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_map));
    
        map = mapFragment.getMap();
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setMyLocationEnabled(false);
    }
    

    For Google Maps Android v2 and fragments, as mentioned above, this is a great resource! Oh, and remember to use the Google Play services SDK!