【问题标题】:After upgrading to google play services 9.4.0 from 9.0.1 getting error on .getMap();从 9.0.1 升级到 google play services 9.4.0 后,.getMap() 出现错误;
【发布时间】:2016-08-12 10:23:16
【问题描述】:

从 9.0.1 升级到 google play services 9.4.0 后,.getMap() 出现错误;如果我返回 google play services 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);
        }
    }
}}

【问题讨论】:

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


【解决方案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() 的帮助,请参阅发布博客文章。

你可以看看Actual guide for using MapFragment

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

【讨论】:

  • 它节省了我的时间。谢谢。
猜你喜欢
  • 2019-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多