【问题标题】:Add marker to map from JSON, Android将标记添加到来自 JSON、Android 的地图
【发布时间】:2017-03-29 15:28:32
【问题描述】:

我试图在从 JSON 获取数据后自动向地图添加标记,位置在 api 中,这就是我所拥有的:

@Override
    protected Void doInBackground(Void... params) {
        HttpHandler sh2 = new HttpHandler();
        final String jsonStrOportunidades = sh2.makeServiceCall(urlOportunidades);
        Log.e(TAG, "Response from URL: " + jsonStrOportunidades);
        if (jsonStrOportunidades != null) {
            try {
                JSONArray array = new JSONArray(jsonStrOportunidades);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    String Designacao = jsonObject.getString("Designacao");
                    String Coord_LAT = jsonObject.getString("Coord_LAT");
                    String Coord_LONG = jsonObject.getString("Coord_LONG");

                    HashMap<String, String> oportunidades = new HashMap<>();

                    oportunidades.put("Designacao", Designacao);
                    oportunidades.put("Coord_LAT", Coord_LAT);
                    oportunidades.put("Coord_LONG", Coord_LONG);

                    double lat1 = Double.parseDouble(Coord_LAT);
                    double lng1 = Double.parseDouble(Coord_LONG);

                    mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));

                    listaOportunidades.add(oportunidades);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                });
            }
        } 
        return null;
    }

“mMap.addMarker”不起作用,甚至可以从那里创建标记吗?

【问题讨论】:

  • 您遇到什么异常或错误?
  • @rafsanahmad007 致命异常:AsyncTask #2
  • @alb 请edit您的问题,logcat 中的完整例外

标签: android json google-maps


【解决方案1】:

尝试从onPostExecute 设置标记。 doInBackground 不是更新 UI 组件的地方。

@Override
protected String doInBackground(Void... params) {
    HttpHandler sh2 = new HttpHandler();
    final String jsonStrOportunidades = sh2.makeServiceCall(urlOportunidades);
    Log.e(TAG, "Response from URL: " + jsonStrOportunidades);

    return jsonStrOportunidades;
}

@Override
protected void onPostExecute(String jsonStrOportunidades){

  if (jsonStrOportunidades != null) {
      try {
          JSONArray array = new JSONArray(jsonStrOportunidades);
          for (int i = 0; i < array.length(); i++) {
              JSONObject jsonObject = array.getJSONObject(i);
              String Designacao = jsonObject.getString("Designacao");
              String Coord_LAT = jsonObject.getString("Coord_LAT");
              String Coord_LONG = jsonObject.getString("Coord_LONG");

              HashMap<String, String> oportunidades = new HashMap<>();

              oportunidades.put("Designacao", Designacao);
              oportunidades.put("Coord_LAT", Coord_LAT);
              oportunidades.put("Coord_LONG", Coord_LONG);

              double lat1 = Double.parseDouble(Coord_LAT);
              double lng1 = Double.parseDouble(Coord_LONG);

              mMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1)));

              listaOportunidades.add(oportunidades);
          }
      } catch (final JSONException e) {
          Log.e(TAG, "Json parsing error: " + e.getMessage());
          Toast.makeText(getApplicationContext(), "Json parsin error: " + e.getMessage(), Toast.LENGTH_LONG).show();
      }
  } 

}

另外,您需要像这样更改 AsyncTask 类

YourAsyncTaskClass extends AsyncTask<String, Void, String>

【讨论】:

  • onPostExecute 已经在 UI 线程上运行。你不需要它来吐司
  • 只需将“protected void doInBackground(Void...params)”更改为“public String doInBackground(Object...params)”即可。谢谢!
  • @cricket_007 对!我真傻。感谢您指出了这一点。和 alb 是的,另一个新手错误。在 Atom 中输入代码,自然它没有出现任何返回类型错误,所以它溜走了我的眼睛。感谢您指出了这一点。我根据你们俩的输入更新了我的代码。
猜你喜欢
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 2015-06-29
  • 2011-04-14
相关资源
最近更新 更多