GoogleMap----android篇(2)

本文介绍了一种基于Google Maps API的地图图层渲染方法,通过解析GeoJSON数据并使用HashMap存储来实现不同指标下国家区域的颜色变化,适用于地理信息系统项目。

因手头项目主要是做地图图层渲染和栅格点。
所以在此讲一下主要的实现思路,因为当时做的时候也是参考各种API和网上搜索相关的教程。
图层渲染:
项目需求,根据不同的指标对不同的国家渲染不同的颜色,比如中国在指标>200范围,要将中国板块渲染成红色
思路:
获取中国板块的geojson数据,也就是中国板块的描边数据,将这些数据进行解析,封装成Map集合,这些数据包括单一图层数据和混合图层数据,然后使用googlemap提供的API添加覆盖图层。
相关代码片段:

    /**
     * 设置单一图层国家数据
     * @param mSimpCountry mSimpCountry
     * @param fileName fileName
     */
    @Override
    public void setSimpCountry(HashMap<String, List<LatLng>> mSimpCountry,
            String fileName)
    {
        this.simpCountry = mSimpCountry;
        if (mSimpCountry != null)
        {
            Iterator<Entry<String, List<LatLng>>> it = mSimpCountry.entrySet().iterator();
            while (it.hasNext())
            {
                Map.Entry<String, List<LatLng>> entry = (Map.Entry<String, List<LatLng>>) it.next();
                List<LatLng> list = entry.getValue();
                String id = entry.getKey();
                if (list != null)
                {
                    if (list.size() > 0)
                    {
                        drawCountryPolygon(list, presenter.getColorWithId(id, fileName));
                    }
                }
            }
            /*            Iterator<String> iterator = mSimpCountry.keySet().iterator();
                        while (iterator.hasNext())
                        {
                            String id = iterator.next();
                            List<LatLng> list = mSimpCountry.get(id);
                            // LogUtil.i(id);
                            if (list != null)
                            {
                                if (list.size() > 0)
                                {
                                    drawCountryPolygon(list,
                                            presenter.getColorWithId(id, fileName));
                                }
                            }
                        }*/
        }
    }

    /**
     * 设置多图层国家数据集合
     * @param multiCountryLatLng 混合图层国家
     * @param fileName 混合图层国家文件
     */
    @Override
    public void setMultiCountry(HashMap<String, List<List>> multiCountryLatLng,
            String fileName)
    {
        this.multiCountry = multiCountryLatLng;
        if (null != multiCountry)
        {
            Iterator<Entry<String, List<List>>> it = multiCountry.entrySet().iterator(); 
            List<LatLng> totalList = new ArrayList<LatLng>();
            while (it.hasNext())
            {
                Map.Entry<String, List<List>> entry = (Map.Entry<String, List<List>>)it.next();
                List<List> list = entry.getValue();
                String id = entry.getKey();
                for (int i = 0; i < list.size(); i++)
                {
                    List<LatLng> list2 = list.get(i);
                    for (int j = 0; j < list2.size(); j++)
                    {
                        totalList.add(list2.get(j));
                    }
                    if (totalList != null)
                    {
                        if (totalList.size() > 0)
                        {
                            drawCountryPolygon(totalList,
                                    presenter.getColorWithId(id, fileName));
                        }
                    }
                    totalList.clear();
                }
            }
        }
        //Iterator<String> multiIterator = ultiCountry.keySet().iterator();
/*        
        while (multiIterator.hasNext())
        {
            String multiId = multiIterator.next();
            List<List> list = ultiCountry.get(multiId);
            for (int i = 0; i < list.size(); i++)
            {
                List<LatLng> list2 = list.get(i);
                for (int j = 0; j < list2.size(); j++)
                {
                    totalList.add(list2.get(j));
                }
                if (totalList != null)
                {
                    if (totalList.size() > 0)
                    {
                        drawCountryPolygon(totalList,
                                presenter.getColorWithId(multiId, fileName));
                    }
                }
                totalList.clear();
            }
        }*/
    }

    /**
     * 绘制图层
     * @param list list
     * @param queryColor queryColor
     */
    @Override
    public void drawCountryPolygon(List<LatLng> list, String queryColor)
    {

        ooPolygon = new PolygonOptions();
        ooPolygon.addAll(list);
        ooPolygon.fillColor(Color.parseColor(queryColor))
                .strokeColor(Color.parseColor("#333333"))
                .strokeWidth(Constants.NUM_0F5);
        if (ooPolygon.getPoints() != null)
        {
            if (ooPolygon.getPoints().size() >= Constants.NUM_2)
            {
                if (mGoogleMap != null)
                {
                    mGoogleMap.addPolygon(ooPolygon);
                }
            }
        }
    }

注意事项:
因为使用的version比较旧,没有地图加载完成的回调,会抛出BitmapDescriptorFactory异常,当时解决这个问题也参考了许多,包括Stack Overflow,还是未能找到解决的办法,因为没办法使用googlemap的OnMapReadyCallback,我自己的解决办法就是在onResume加强判断,initMap可以在onCreate中调用

 @Override
    public void onResume()
    {
        super.onResume();
        if (mGoogleMap != null)
        {
            mGoogleMap.clear();
            initMapSetting(mGoogleMap);
        }
        mGoogleMap = mMap.getMap();
        initMapSetting(mGoogleMap);
        if (simpCountry != null && simpCountry.size() > 0
                && multiCountry != null && multiCountry.size() > 0)
        {
            setSimpCountry(simpCountry, quota);
            setMultiCountry(multiCountry, quota);
        }
        else
        {
            // 初始化图层
            initPolygon(quota);
        }
        // 设置地图监听
        setMapListener();
    }*
     * 初始化地图相关
     */

private void initMap()
{

    mMap = SupportMapFragment.newInstance();
    this.getActivity().getSupportFragmentManager().beginTransaction()
            .replace(R.id.map_container, mMap).commit();
    mGoogleMap = mMap.getMap();
    if (mGoogleMap != null)
    {
        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        // mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
        setMapListener();
        initMapSetting(mGoogleMap);
    }
}
    /**
 * 设置地图相关
 * @param googleMap googleMap
 */
private void initMapSetting(GoogleMap googleMap)
{
    UiSettings settings = googleMap.getUiSettings();
    settings.setCompassEnabled(false);
    settings.setZoomControlsEnabled(true);
    settings.setAllGesturesEnabled(true);
    settings.setRotateGesturesEnabled(false);
    settings.setTiltGesturesEnabled(false);
}
布局文件:
<FrameLayout
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

此问题得以解决
下一篇会继续讲解地图的点击事件和栅格点优化方案

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值