11

I'm working on integrating Google Maps into the app I'm working on and I've had a rather unpleasant time doing it thus far. Regardless, I finally got a SupportMapFragment displaying a map and set a location and zoom level.

Here is the functional bits of my code thus far:

@Override
public void onActivityCreated( Bundle savedInstanceState ) {

    super.onActivityCreated( savedInstanceState );

    Location location = BundleChecker.getExtraOrThrow( KEY_LOCATION, new Bundle[] { savedInstanceState, getArguments() } );
    setLocation( location );

    if ( checkGooglePlayServicesStatus() == ConnectionResult.SUCCESS ) {
        setMapFragment( new SupportMapFragment() );
        getActivity().getSupportFragmentManager().beginTransaction().add( R.id.location_detail_mapFrame, getMapFragment() ).commit();

    }

    populateAddress();
    attachButtonListeners();

    Runnable initMap = new Runnable() {

        @Override
        public void run() {

            if ( checkGooglePlayServicesStatus() == ConnectionResult.SUCCESS ) {
                try {
                    GoogleMap map = getMapFragment().getMap();
                    LatLng latLng = getLocation().getAddress().getLatLng( getActivity() );
                    CameraUpdate update = CameraUpdateFactory.newLatLngZoom( latLng, DEFAULT_MAP_ZOOM );
                    map.animateCamera( update );
                }
                catch (IOException e) {
                    Log.e( TAG, e.getMessage(), e );
                    Toast.makeText( getActivity(), "Unable to find location", Toast.LENGTH_SHORT ).show();
                }
            }

        }
    };

    Handler handler = new Handler();
    handler.postDelayed( initMap, 200 );
}

Also, I wrote a simple convenience method to get a LatLng from my Address model that you may criticize as well:

/*
 * Convenience method to easily check if there is a valid lat & lng in this address
 */
public boolean hasLatLng() {

    return getLatitude() != null && getLongitude() != null;
}

/*
 * Convenience method for use with Google Maps API
 */
public LatLng getLatLng( Context context ) throws IOException {

    LatLng latLng = null;
    if ( hasLatLng() ) {
        latLng = new LatLng( getLatitude(), getLongitude() );
    }
    else {
        String locationString = getStreet() + ", " + AddressUtil.makeCityStateZipString( this );
        Geocoder geoCoder = new Geocoder( context );
        try {
            List<android.location.Address> matches = geoCoder.getFromLocationName( locationString, 2 );
            if ( matches != null && matches.size() > 0 ) {
                double lat = matches.get( 0 ).getLatitude();
                double lng = matches.get( 0 ).getLongitude();
                latLng = new LatLng( lat, lng );
            }
        }
        catch (IOException e) {
            throw new IOException( e );
        }
    }

    return latLng;
}

I'm aware that this code is not ideal and needs to be refactored. This is my first time working with Google Maps so please feel free to offer suggestions as to how I might do that as well. I experienced a lot of problems when trying to use the MapFragment as a in my layout XML, so I'm creating it programmatically.

The heart of the matter: I was getting some bogus address data from the staging server and this resulted in the Address#getLatLng method returning null which caused an exception when calling CameraUpdateFactory.newLatLngZoom. After I got this exception, I was no longer able to get map data from Google. The map fragment is blank now and messages are displayed in logcat:

SupportMapFragment with no map content

05-21 18:11:42.903: I/Google Maps Android API(15747): Failed to contact Google servers. Another attempt will be made when connectivity is established.

05-21 18:11:43.093: E/Google Maps Android API(15747): Failed to load map. Error contacting Google servers. This is probably an authentication issue (but could be due to network errors).

I have created a new api key and replaced the current one in my manifest with no change. The only changes I had made to the above code were to account for a null LatLng and I have since undone those changes in a pointless attempt to get my code back to a functional state.

Additionally, to make things a bit stranger, I built the sample maps project that is included with the Google Play Services Extras and it works perfectly (has a separate API key, btw).

What might I have done wrong here? Am I overlooking something obvious?

Community
  • 1
  • 1
dm78
  • 1,570
  • 1
  • 16
  • 28
  • https://developers.google.com/maps/documentation/android/ – kongkea May 22 '13 at 14:25
  • What version of google maps are you using? Also, are you using debugging key or release key for maps? – MiStr May 22 '13 at 14:33
  • 6
    @kongkea is there something useful you intend with your link? Posting a link to the maps developers documentation and saying nothing else is useless. – dm78 May 22 '13 at 14:49
  • @MiStr I'm using the current version of the google maps API for Android: v2. – dm78 May 22 '13 at 14:50
  • Are you testing application or your application is on Google Play? Try creating new API key. – Sharjeel May 22 '13 at 15:54
  • @Sharj My app is not on Google Play. I mentioned in my orignal post that I have created a new api key. I have done this multiple times with no change. – dm78 May 22 '13 at 16:56

3 Answers3

19

This problem is usually derived from a problem in referencing google-play-service library. Take a look at this blog post I wrote on how to integrate Google Maps in your application, especially the first 3 steps:

Google Maps API V2

another cause of this could be that you haven't configured the Google API Console properly, so I suggest you to take a look at this guide as well:

Google Maps API V2 Key

another reason that may cause this is if you have some kind of problem in your permissions in the manifest file. You can look at the first guide for the needed permissions as well.

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • 5
    Thank you. Apparently a merge conflict caused me to lose from my manifest. – dm78 May 22 '13 at 18:14
  • 4
    if somebody still looking for an answer, check here - the same problem: http://stackoverflow.com/a/17947755/1891118 – Oleksii K. Jul 30 '13 at 13:08
  • for me what worked was to change the target-sdk to the newest one (18 when writing this) + update the google play services lib – Rotem Sep 04 '13 at 00:56
2

Use Something like this:

  1. Update the google play services in the SDK.

  2. Manually uninstall the App from device and restart the device. i have tried it and its going perfectly bro

Also do one thing get the new api key to edited the new sh1 code from https://code.google.com/apis/console/

you can get your sh1 code from window- preference-android-buid

Ravind Maurya
  • 977
  • 15
  • 24
  • Hopefully your answer will help someone else. In my case, it was simply a permission that was not present any longer. – dm78 Aug 29 '13 at 13:27
0

Making Google maps work is a Googlemare. This worked for me:

-Update Google play services with the Android Manager -Make a fresh apikey with: Sha1 keystore (Window->preferences->Android->Build) and project package name. Do this at: https://code.google.com/apis/console/

Somebody had changed the package name and was foiling the app!!

Try this before you need anger management therapy thanks to google maps.

Josh
  • 6,251
  • 2
  • 46
  • 73