2013-02-10 38 views
8

這是我的asynctask部分,我將pin放在從xml解析的某個位置。對於一個小的位置數據集這樣做很好,但是當位置數據集很大時需要很長時間。如何在android中顯示mapview的可見部分的位置引腳?

private class MapLoader extends AsyncTask<Void, Void, Void> { 
     ProgressDialog dialog; 
     ArrayList<POI>[] mappoiList; 

     @Override 
     protected void onPreExecute() { 
      mapOverlays = mapview.getOverlays(); 
      dialog = new ProgressDialog(MainActivity.this); 
        dialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 

      try { 

       URL url = new URL(MainActivity.baseUrl 
         + "latitude=40.9192799&longitude=-74.0657508&distance=20"); 


       SAXParserFactory spf = SAXParserFactory.newInstance(); 
       SAXParser sp = spf.newSAXParser(); 
       XMLReader xr = sp.getXMLReader(); 
       ParserHandler parserHandler = new ParserHandler(); 
       xr.setContentHandler(parserHandler); 
       xr.parse(new InputSource(url.openStream())); 
       Log.d("Thread", url.toString()); 
       basicItems = parserHandler.getBasicItems(); 
       featuredItems = parserHandler.getFeaturedItems(); 
       restaurants = parserHandler.getRestaurants(); 
       mapvisibleList = new ArrayList<POI>(); 
       mappoiList = new ArrayList[2]; 
       mappoiList[0] = new ArrayList<POI>(); 
       mappoiList[1] = new ArrayList<POI>(); 

       for (int i = 0; i < featuredItems.size(); i++) { 

        POI poi = new POI(featuredItems.get(i), 1); 
        mappoiList[0].add(poi); 
       } 
       for (int i = 0; i < restaurants.size(); i++) { 

        POI poi = new POI(restaurants.get(i), 2); 
        mappoiList[0].add(poi); 
       } 
       for (int i = 0; i < basicItems.size(); i++) { 

        POI poi = new POI(basicItems.get(i), 0); 
        mappoiList[1].add(poi); 
       } 

       for (int i = 0; i < mappoiList[0].size(); i++) { 
        if (mappoiList[0] != null) { 
         mapvisibleList.add(mappoiList[0].get(i)); 
        } 
       } 

       for (int i = 0; i < mappoiList[1].size(); i++) { 
        if (mappoiList[1] != null) { 
         mapvisibleList.add(mappoiList[1].get(i)); 
        } 
       } 
       for (FeaturedItem item : featuredItems) { 
        Log.d("FEATURED", 
          item.getName() + "Distance: " + item.getDistance()); 
       } 
       for (Restaurant item : restaurants) { 
        Log.d("RESTAURANTS", 
          item.getName() + "Distance: " + item.getDistance()); 
       } 
       for (BasicItem item : basicItems) { 
        Log.d("BASIC", 
          item.getName() + "Distance: " + item.getDistance()); 
       } 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
       showErrorDialog("Error", "Malformed URL Error Occurred"); 
      } catch (ParserConfigurationException e) { 
       e.printStackTrace(); 
       showErrorDialog("Error", 
         "Parser Configuration Problem Occurred"); 
      } catch (SAXException e) { 
       e.printStackTrace(); 
       showErrorDialog("Error", "SAX Parser Error Occurred"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       showErrorDialog("Error", "IOException Occurred"); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      GlobalData.gBasicItems = basicItems; 
      GlobalData.gFeaturedItems = featuredItems; 
      GlobalData.gRestaurants = restaurants; 

      if (currentLocation != null) { 
       int curLat = (int) (currentLocation.getLatitude() * 1e6); 
       int curLon = (int) (currentLocation.getLongitude() * 1e6); 
       GeoPoint gp = new GeoPoint(curLat, curLon); 
       mapControl.animateTo(gp); 

       mapControl.setZoom(14); 
       OverlayItem item = new OverlayItem(gp, "My Location", 
         "My Location"); 

       itemizedOverlay1 = new MyItemizedOverlay(drawableFeature, 
         MainActivity.this, mappoiList[0]); 
       itemizedOverlay2 = new MyItemizedOverlay(drawableBasic, 
         MainActivity.this, mappoiList[1]); 
       itemizedOverlay3 = new MyItemizedOverlay(drawableCurrent, 
         MainActivity.this, mappoiList[0]); 
       Log.i("asyncbasic", "" + basicItems.size()); 
       Log.i("asyncfeatured", "" + featuredItems.size()); 
       Log.i("asyncres", "" + restaurants.size()); 
      if (featuredItems != null) { 
        int featuredLength = featuredItems.size(); 
        for (int i = 0; i < featuredLength; i++) { 
         FeaturedItem fItem = featuredItems.get(i); 
         int lat = (int) (Double 
           .parseDouble(fItem.getLatitude()) * 1e6); 
         int lon = (int) (Double.parseDouble(fItem 
           .getLongitude()) * 1e6); 
         OverlayItem oItem = new OverlayItem(new GeoPoint(lat, 
           lon), fItem.getName(), "Feature"); 
         itemizedOverlay1.addOverlay(oItem); 
         Log.i("Map over lay", "Finished one featured"); 
        } 
       } 
       if (basicItems != null) { 
        int basicLength = basicItems.size(); 
        for (int i = 0; i < basicLength; i++) { 
         BasicItem bItem = basicItems.get(i); 
         int lat = (int) (Double 
           .parseDouble(bItem.getLatitude()) * 1e6); 
         int lon = (int) (Double.parseDouble(bItem 
           .getLongitude()) * 1e6); 
         OverlayItem oItem = new OverlayItem(new GeoPoint(lat, 
           lon), bItem.getName(), "Basic"); 
         itemizedOverlay2.addOverlay(oItem); 
         Log.i("Map over lay", "Finished one Basic"); 

        } 
       } 
       if (restaurants != null) { 
        int resLength = restaurants.size(); 
        for (int i = 0; i < resLength; i++) { 
         Restaurant res = restaurants.get(i); 
         int lat = (int) (Double.parseDouble(res.getLatitude()) * 1e6); 
         int lon = (int) (Double.parseDouble(res.getLongitude()) * 1e6); 
         OverlayItem oItem = new OverlayItem(new GeoPoint(lat, 
           lon), res.getName(), "Restaurant"); 
         itemizedOverlay1.addOverlay(oItem); 
         Log.i("Map over lay", "Finished one Restaurant"); 
        } 
       } 

       itemizedOverlay3.addOverlay(item); 

      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 

      mapOverlays.clear(); 

      mapOverlays.add(itemizedOverlay1); 
      mapOverlays.add(itemizedOverlay2); 
      mapOverlays.add(itemizedOverlay3); 
      mapview.postInvalidate(); 
      dialog.dismiss(); 

     } 

    } 

我學習瞭如何使用getLatitudeSpangetLongitudeSpan擺脫這個問題,但我不是在哪裏和如何在我的異步任務使用它太清楚。有什麼建議麼 ?

+0

任何人都可以幫忙嗎?我沒有理由爲什麼有人沒有至少給出一個單一的答覆:( – Reyjohn 2013-02-16 21:51:07

+0

嘗試分析你的應用程序,看看時間被吃掉了,你能定義'大數'嗎?即這是10或1000? – 2013-02-19 10:27:23

+0

1)避免使用這麼多的循環嘗試將它們結合起來。 2)優化你的'ParserHandler'以最終形式返回數據('ArrayList '和'new ArrayList [2]'),以避免多次運行DataSet! – madlymad 2013-02-21 11:03:42

回答

0

請給出確切的數據大小,其中需要的時間很長,也可以嘗試分析代碼以查看每個代碼塊所花費的時間。

但看的代碼中,通過所有這些變量循環會對時間顯著影響,如果對增加正在操作的數據,

儘量簡化/組合的循環,

例如,這個循環

 for (int i = 0; i < featuredItems.size(); i++) { 

       POI poi = new POI(featuredItems.get(i), 1); 
       mappoiList[0].add(poi); 
      } 

而這一次

 if (featuredItems != null) { 
        int featuredLength = featuredItems.size(); 
        for (int i = 0; i < featuredLength; i++) { 
        FeaturedItem fItem = featuredItems.get(i); 
        int lat = (int) (Double 
          .parseDouble(fItem.getLatitude()) * 1e6); 
        int lon = (int) (Double.parseDouble(fItem 
          .getLongitude()) * 1e6); 
        OverlayItem oItem = new OverlayItem(new GeoPoint(lat, 
          lon), fItem.getName(), "Feature"); 
        itemizedOverlay1.addOverlay(oItem); 
        Log.i("Map over lay", "Finished one featured"); 
       } 
      } 

不能合併?

也是這個循環

for (int i = 0; i < basicItems.size(); i++) { 

       POI poi = new POI(basicItems.get(i), 0); 
       mappoiList[1].add(poi); 
       } 

 if (basicItems != null) { 
       int basicLength = basicItems.size(); 
       for (int i = 0; i < basicLength; i++) { 
        BasicItem bItem = basicItems.get(i); 
        int lat = (int) (Double 
          .parseDouble(bItem.getLatitude()) * 1e6); 
        int lon = (int) (Double.parseDouble(bItem 
          .getLongitude()) * 1e6); 
        OverlayItem oItem = new OverlayItem(new GeoPoint(lat, 
          lon), bItem.getName(), "Basic"); 
        itemizedOverlay2.addOverlay(oItem); 
        Log.i("Map over lay", "Finished one Basic"); 

       } 
      } 

也可以合併,

0

我想問題是不是疊加。您正在使用sax解析器獲取所有項目,這正在縮短您的訓練時間。您試圖獲取所有元素,然後將其添加到地圖。如果您修改代碼以獲得單個元素更新以覆蓋並填充它,這可能會加快執行速度。 如果您仍然遇到同樣的問題,請嘗試使用疊加層的單獨線程或畫布對象(通過投影地理點)添加元素。

0

您可能想要看看實現生產者 - 消費者模型,其中消費者將是地圖本身,生產者將成爲生成引腳信息的邏輯。

0

您正在使用一個sax解析器這非常耗時,請使用像json這樣的代表性對象模型,這可能很容易解析,因此可以繪製在地圖上。

//很少有建議可能會減少您面臨的時間問題。

  1. 只有繪製銷和點擊彈出一個顯示裏面進一步的細節。
相關問題