如何在android中显示mapview的可见部分的位置图钉?

时间:2013-02-10 12:57:39

标签: java android android-maps

这是我的asynctask部分,我将pin放在从xml解析的某个位置。这对于小型位置数据集来说很好,但是当位置数据集很大时需要很长时间。

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来解决这个问题,但我不太清楚在异步任务中何处以及如何使用它。有什么建议吗?

4 个答案:

答案 0 :(得分: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");

                }
            }

也可以合并,

答案 1 :(得分:0)

我认为问题不在于叠加。你正在使用sax解析器来获取所有项目,这将花费你的时间。您尝试获取所有元素,然后将其添加到地图中。如果您修改代码,因为您需要覆盖单个元素更新并填充它,这可能会加快执行速度。 如果您仍然遇到同样的问题,请尝试使用叠加的单独线程或画布对象添加元素(通过投影地理点)。

答案 2 :(得分:0)

您可能希望了解如何实现Producer-Consumer模型,其中使用者将是地图本身,生产者将是为引脚生成信息的逻辑。

答案 3 :(得分:0)

您正在使用非常耗时的 sax解析器,使用像json这样的对象模型,可以很容易地解析,因此可以在地图上绘制。

//很少有建议本身可以减少您面临的时间问题。

  1. 仅绘制图钉,然后单击打开弹出窗口以显示其中的更多详细信息。