从9.0.1升级到谷歌播放服务9.4.0后获取.getMap();

时间:2016-08-12 10:23:17

标签: java android google-maps google-maps-api-3

升级到谷歌播放服务9.4.0从9.0.1获取错误.getMap();如果我做谷歌播放服务9.0.1回来。我的应用程序在启动时崩溃了......请大家帮帮我......我现在能做什么......提前做好...

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);
        }
    }
}}

1 个答案:

答案 0 :(得分:1)

MapFragment.getMap()自2014年12月起不再使用,随着2016年6月27日的新版本,getMap()已被删除。现在好的(和唯一的)方法是使用getMapAsync()。

有关详细信息,请参阅Maps Android API Release Notes

  

以前不推荐使用的getMap()函数在Google Play服务SDK中不再可用。 (它仍然可以在提供给Android设备的Google Play服务APK中使用。)自2014年12月以来,不推荐使用getMap()函数。有关从getMap()转换为getMapAsync()的帮助,请参阅发布博客文章。 / p>

您可以查看Actual guide for using MapFragment

如果从getMap()切换到getMapAsync()时遇到问题,也可以查看Geo Developers Blog