1

After upgrading to google play services 9.4.0 from 9.0.1 getting error on .getMap();if i do google play services 9.0.1 back. my app crashes on startup...please guys help me out..want i can do now...thnks in advance...

public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();

}
private void setUpMapIfNeeded() {
    if (mMap == null) {
        MapFragment mapFragment = (MapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        mMap = mapFragment.getMap();
        if (mMap != null) {
            new MarkerTask().execute();
        }
    }
}

private class MarkerTask extends AsyncTask<Void, Void, String> {

    private static final String LOG_TAG = "ExampleApp";
    private static final String SERVICE_URL = "http://www.xxxxxxx.com/bar/mob/map.php?city=&location=";

    // Invoked by execute() method of this object
    @Override
    protected String doInBackground(Void... args) {

        HttpURLConnection conn = null;
        final StringBuilder json = new StringBuilder();
        try {
            // Connect to the web service
            URL url = new URL(SERVICE_URL);
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Read the JSON data into the StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                json.append(buff, 0, read);
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error connecting to service", e);
            //throw new IOException("Error connecting to service", e); //uncaught
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return json.toString();
    }

    // Executed after the complete execution of doInBackground() method
    @Override
    protected void onPostExecute(String json) {

        try {
            // De-serialize the JSON string into an array of city objects
            JSONArray jsonArray = new JSONArray(json);
            System.out.println("Returned value " + jsonArray.toString());
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObj = jsonArray.getJSONObject(i);
                LatLng latLng = new LatLng(jsonObj.getJSONArray("latlng").getDouble(0),
                        jsonObj.getJSONArray("latlng").getDouble(1));

                //move CameraPosition on first result
                if (i == 0) {
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(latLng).zoom(13).build();

                    mMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }

                // Create a marker for each city in the JSON data.
                mMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                        .title(jsonObj.getString("barname"))
                        .snippet(jsonObj.getString("address"))
                        .position(latLng));
            }
        } catch (JSONException e) {
            Log.e(LOG_TAG, "Error processing JSON", e);
        }
    }
}}
ArK
  • 20,698
  • 67
  • 109
  • 136
Abhi
  • 43
  • 2
  • http://stackoverflow.com/a/38817480/5456493 Check out this answer for more help – Abhishek Aug 12 '16 at 11:04
  • Possible duplicate of [Replace getMap with getMapAsync](http://stackoverflow.com/questions/31371865/replace-getmap-with-getmapasync) – N Dorigatti Aug 13 '16 at 12:38

1 Answers1

1

MapFragment.getMap() is deprecated since December 2014, and with the new release of june 27 2016, getMap() has been deleted. Now the good(and unique) way is to use getMapAsync().

See Maps Android API Release Notes for more details :

The previously-deprecated getMap() function is no longer available in the Google Play services SDK. (It is still available in the Google Play services APK that is delivered to Android devices.) The getMap() function has been deprecated since December 2014. See the release blog post for help with converting from getMap() to getMapAsync().

You can take a look at the Actual guide for using MapFragment

You can also look at the Geo Developers Blog if you have problems to switch from getMap() to getMapAsync().

Nutriz
  • 189
  • 2
  • 12