MapView并发修改异常

发布于 2024-09-07 11:56:21 字数 4874 浏览 1 评论 0原文

正如标题所示,我在尝试在两个位置之间创建路线时(有时)出现并发修改异常...... 这是我的代码(如果您想知道 MyOverlay 不会尝试访问地图中的其他叠加层)

    private class fillRouteTask extends AsyncTask<Void, GeoPoint, Void> {

  /**
   * create the url to call to get the route
   * 
   * @param src
   * @param dest
   * @return
   */
  private StringBuilder createUrl(GeoPoint src, GeoPoint dest) {
   // connect to map web service
   StringBuilder urlString = new StringBuilder();
   urlString.append("http://maps.google.com/maps?f=d&hl=en");
   urlString.append("&saddr=");// from
   urlString.append(Double
     .toString((double) src.getLatitudeE6() / 1.0E6));
   urlString.append(",");
   urlString.append(Double
     .toString((double) src.getLongitudeE6() / 1.0E6));
   urlString.append("&daddr=");// to
   urlString.append(Double
     .toString((double) dest.getLatitudeE6() / 1.0E6));
   urlString.append(",");
   urlString.append(Double
     .toString((double) dest.getLongitudeE6() / 1.0E6));
   urlString.append("&ie=UTF8&0&om=0&output=kml");
   Log.d("xxx", "URL=" + urlString.toString());

   return urlString;
  }

  /**
   * create the connection to google url
   * 
   * @param src
   * @param dest
   * @return
   */
  private String connectToUrl(GeoPoint src, GeoPoint dest) {

   // get the kml (XML) doc. And parse it to get the
   // coordinates(direction
   // route).
   Document doc = null;
   HttpURLConnection urlConnection = null;
   URL url = null;
   try {
    url = new URL(createUrl(src, dest).toString());
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();

    DocumentBuilderFactory dbf = DocumentBuilderFactory
      .newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    doc = db.parse(urlConnection.getInputStream());

    if (doc.getElementsByTagName("GeometryCollection") != null
      && doc.getElementsByTagName("GeometryCollection")
        .getLength() > 0) {
     return doc.getElementsByTagName("GeometryCollection").item(
       0).getFirstChild().getFirstChild().getFirstChild()
       .getNodeValue();
    }

   } catch (MalformedURLException e) {
    Log.d("test", e.getLocalizedMessage());
   } catch (IOException e) {
    Log.d("test", e.getLocalizedMessage());
   } catch (ParserConfigurationException e) {
    Log.d("test", e.getLocalizedMessage());
   } catch (SAXException e) {
    Log.d("test", e.getLocalizedMessage());
   }
   return null;
  }

  protected Void doInBackground(Void... arg0) {
   try {
    // get the current overlays present in the map
    List<Overlay> overs = Collections.synchronizedList(mapView
      .getOverlays());
    String path = connectToUrl(orig, dest);
    if (path != null) {
     Log.d("xxx", "path=" + path);
     String[] pairs = path.split(" ");
     String[] lngLat = pairs[0].split(",");

     GeoPoint startGP = new GeoPoint((int) (Double
       .parseDouble(lngLat[1]) * 1E6), (int) (Double
       .parseDouble(lngLat[0]) * 1E6));

     overs.add(new MyOverlay(startGP, startGP, 1));
     GeoPoint gp1;
     GeoPoint gp2 = startGP;
     for (int i = 1; i < pairs.length; i++) // the last one would
     // be
     // crash
     {
      lngLat = pairs[i].split(",");
      gp1 = gp2;
      // watch out! For GeoPoint, first:latitude,
      // second:longitude
      gp2 = new GeoPoint(
        (int) (Double.parseDouble(lngLat[1]) * 1E6),
        (int) (Double.parseDouble(lngLat[0]) * 1E6));
      Log.d("xxx", "pair:" + pairs[i]);
      publishProgress(gp1, gp2);
     }
     overs.add(new MyOverlay(dest, dest, 3)); // use
     // the
     // default
     // color
     drawn = false;
    } else {
     m_handler.post(new Runnable() {

      public void run() {
       Toast.makeText(getApplicationContext(),
         "Problem in getting the directions",
         Toast.LENGTH_SHORT).show();
      }
     });
    }
   } catch (Exception e) {
    m_handler.post(new Runnable() {

     public void run() {
      Toast.makeText(getApplicationContext(),
        R.string.errorrouteLoad, Toast.LENGTH_LONG)
        .show();
     }
    });
   }
   return null;

  }

  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onProgressUpdate(Progress[])
   */
  @Override
  protected void onProgressUpdate(GeoPoint... values) {
   // TODO Auto-generated method stub
   List<Overlay> overs = Collections.synchronizedList(mapView
     .getOverlays());
   overs.add(new MyOverlay(values[0], values[1], 2, 0x0000ff));

  }

  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
   */
  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   Log.d("routing", "done");
   mapView.postInvalidate();
  }
 }

As the title suggest i have (sometimes) a concurrentmodificationexception while trying to create a route between two locations ...
Here is my code (and in case you're wondering MyOverlay does not try to access the other Overlays in the map)

    private class fillRouteTask extends AsyncTask<Void, GeoPoint, Void> {

  /**
   * create the url to call to get the route
   * 
   * @param src
   * @param dest
   * @return
   */
  private StringBuilder createUrl(GeoPoint src, GeoPoint dest) {
   // connect to map web service
   StringBuilder urlString = new StringBuilder();
   urlString.append("http://maps.google.com/maps?f=d&hl=en");
   urlString.append("&saddr=");// from
   urlString.append(Double
     .toString((double) src.getLatitudeE6() / 1.0E6));
   urlString.append(",");
   urlString.append(Double
     .toString((double) src.getLongitudeE6() / 1.0E6));
   urlString.append("&daddr=");// to
   urlString.append(Double
     .toString((double) dest.getLatitudeE6() / 1.0E6));
   urlString.append(",");
   urlString.append(Double
     .toString((double) dest.getLongitudeE6() / 1.0E6));
   urlString.append("&ie=UTF8&0&om=0&output=kml");
   Log.d("xxx", "URL=" + urlString.toString());

   return urlString;
  }

  /**
   * create the connection to google url
   * 
   * @param src
   * @param dest
   * @return
   */
  private String connectToUrl(GeoPoint src, GeoPoint dest) {

   // get the kml (XML) doc. And parse it to get the
   // coordinates(direction
   // route).
   Document doc = null;
   HttpURLConnection urlConnection = null;
   URL url = null;
   try {
    url = new URL(createUrl(src, dest).toString());
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.connect();

    DocumentBuilderFactory dbf = DocumentBuilderFactory
      .newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    doc = db.parse(urlConnection.getInputStream());

    if (doc.getElementsByTagName("GeometryCollection") != null
      && doc.getElementsByTagName("GeometryCollection")
        .getLength() > 0) {
     return doc.getElementsByTagName("GeometryCollection").item(
       0).getFirstChild().getFirstChild().getFirstChild()
       .getNodeValue();
    }

   } catch (MalformedURLException e) {
    Log.d("test", e.getLocalizedMessage());
   } catch (IOException e) {
    Log.d("test", e.getLocalizedMessage());
   } catch (ParserConfigurationException e) {
    Log.d("test", e.getLocalizedMessage());
   } catch (SAXException e) {
    Log.d("test", e.getLocalizedMessage());
   }
   return null;
  }

  protected Void doInBackground(Void... arg0) {
   try {
    // get the current overlays present in the map
    List<Overlay> overs = Collections.synchronizedList(mapView
      .getOverlays());
    String path = connectToUrl(orig, dest);
    if (path != null) {
     Log.d("xxx", "path=" + path);
     String[] pairs = path.split(" ");
     String[] lngLat = pairs[0].split(",");

     GeoPoint startGP = new GeoPoint((int) (Double
       .parseDouble(lngLat[1]) * 1E6), (int) (Double
       .parseDouble(lngLat[0]) * 1E6));

     overs.add(new MyOverlay(startGP, startGP, 1));
     GeoPoint gp1;
     GeoPoint gp2 = startGP;
     for (int i = 1; i < pairs.length; i++) // the last one would
     // be
     // crash
     {
      lngLat = pairs[i].split(",");
      gp1 = gp2;
      // watch out! For GeoPoint, first:latitude,
      // second:longitude
      gp2 = new GeoPoint(
        (int) (Double.parseDouble(lngLat[1]) * 1E6),
        (int) (Double.parseDouble(lngLat[0]) * 1E6));
      Log.d("xxx", "pair:" + pairs[i]);
      publishProgress(gp1, gp2);
     }
     overs.add(new MyOverlay(dest, dest, 3)); // use
     // the
     // default
     // color
     drawn = false;
    } else {
     m_handler.post(new Runnable() {

      public void run() {
       Toast.makeText(getApplicationContext(),
         "Problem in getting the directions",
         Toast.LENGTH_SHORT).show();
      }
     });
    }
   } catch (Exception e) {
    m_handler.post(new Runnable() {

     public void run() {
      Toast.makeText(getApplicationContext(),
        R.string.errorrouteLoad, Toast.LENGTH_LONG)
        .show();
     }
    });
   }
   return null;

  }

  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onProgressUpdate(Progress[])
   */
  @Override
  protected void onProgressUpdate(GeoPoint... values) {
   // TODO Auto-generated method stub
   List<Overlay> overs = Collections.synchronizedList(mapView
     .getOverlays());
   overs.add(new MyOverlay(values[0], values[1], 2, 0x0000ff));

  }

  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
   */
  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   Log.d("routing", "done");
   mapView.postInvalidate();
  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶视觉 2024-09-14 11:56:21

您只能在 UI 线程上修改叠加层。您需要在 AsyncTask 的 onPostExecute 方法中将新数据添加到叠加层中。

You can only modify overlays on the UI thread. You need to add the new data to the overlay in the onPostExecute method of AsyncTask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文