因手头项目主要是做地图图层渲染和栅格点。
所以在此讲一下主要的实现思路,因为当时做的时候也是参考各种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" />
此问题得以解决
下一篇会继续讲解地图的点击事件和栅格点优化方案
本文介绍了一种基于Google Maps API的地图图层渲染方法,通过解析GeoJSON数据并使用HashMap存储来实现不同指标下国家区域的颜色变化,适用于地理信息系统项目。
3181

被折叠的 条评论
为什么被折叠?



