从9.0.1升级到Google Play服务9.4.0后,在.getMap()上获取错误;如果我这样做,则Google Play服务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);
        }
    }
}}

最佳答案

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

有关更多详细信息,请参见Maps Android API Release Notes


  Google Play服务SDK中不再提供以前不推荐使用的getMap()函数。 (仍可从提供给Android设备的Google Play服务APK中获得该功能。)自2014年12月起,不赞成使用getMap()函数。有关将getMap()转换为getMapAsync()的帮助,请参见发布博客。


您可以看一下Actual guide for using MapFragment

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

关于java - 从9.0.1升级到Google Play服务9.4.0后,.getMap()出现错误;,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38915645/

10-12 01:14