最近进行的项目需要嵌入高德地图,并且需要在地图上画点、画面,点是好画但是面呢,有的大有的小,我需要设置面的可视范围,否则有些面在视野之内有些面在视野之外,所以,在这点上费了点力气。现在将比较完整的代码整理如下:
http://lbs.amap.com/api/android-sdk/guide/create-project/dev-attention
首先按照相关配置配置成功
if (aMap == null) {
aMap = mapview.getMap();
// //绘制厂区图
setComMap(comPointBean);
// //初始化地图点标记
initMapData(comPointBean);
}
/**
* date: 2017/7/28 17:41
* autour: HelenChen
* description: 绘制厂区图
*/
private void setComMap(ComPointBean comPointBean) {
if (!StrUtils.isEmpty(comPointBean.getTargetInfo().getCoordinateSet())) {
String coordinateSet = comPointBean.getTargetInfo().getCoordinateSet();
try {
//经纬度 longitude latitude
List longitude = new ArrayList();
List latitude = new ArrayList();
JSONArray array = new JSONArray(coordinateSet);
JSONArray array1 = new JSONArray(array.getString(0));
JSONArray array2 = new JSONArray(array1.getString(0));
JSONArray array3 = new JSONArray(array2.toString());
for (int i = 0; i < array3.length(); i++) {
JSONArray array4 = new JSONArray(array3.getString(i));
longitude.add(array4.getString(0));
latitude.add(array4.getString(1));
}
// 声明 多边形参数对象
PolygonOptions polygonOptions = new PolygonOptions();
LatLng latLng = new LatLng(Double.parseDouble(latitude.get(0)), Double.parseDouble(longitude.get(0)));//给默认值 纬度、经度
LatLngBounds bounds = new LatLngBounds(latLng, latLng);
for (int j = 0; j < longitude.size(); j++) {
// 定义多边形的每个点点坐标
latLng = new LatLng(Double.parseDouble(latitude.get(j)), Double.parseDouble(longitude.get(j)));//纬度、经度
// 添加 多边形的每个顶点(顺序添加)
polygonOptions.add(latLng);
//添加边界
bounds = bounds.including(latLng);
}
mapview.getMap().addPolygon(polygonOptions.fillColor(Color.argb(50, 1, 1, 1)).strokeColor(Color.argb(50, 1, 1, 1)).strokeWidth(1));
//添加边界
float dimenBottom = getResources().getDimension(R.dimen.dimen140);
float dimenLeft = getResources().getDimension(R.dimen.dimen20);
int paddingBottom = Math.round(dimenBottom);
int paddingLeft = Math.round(dimenLeft);
// int paddingLeft,paddingRight,paddingTop,paddingBottom
mapview.getMap().animateCamera(CameraUpdateFactory.newLatLngBoundsRect(bounds, paddingLeft, paddingLeft, paddingLeft, paddingBottom));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
/**
* date: 2017/8/3 10:28
* autour: HelenChen
* description: 初始化地图点标记
*/
private void initMapData(ComPointBean comPointBean) {
ArrayList optionsArrayList = new ArrayList<>();
int i = 0;
for (int j = 0; j < comPointBean.getOtherInfo().size(); j++) {
if (!StrUtils.isEmpty(comPointBean.getOtherInfo().get(j).getImgName())) {
String imageName = comPointBean.getOtherInfo().get(j).getImgName();
String lowerImageName = imageName.toLowerCase();
switch (lowerImageName) {
case "atmosphere_over.gif":
lowerImageName = "atmosphere_over_gif";
break;
case "dust_overdata.gif":
lowerImageName = "dust_overdata_gif";
break;
case "exhaust_over.gif":
lowerImageName = "exhaust_over_gif";
break;
case "ministation_over.gif":
lowerImageName = "ministation_over_gif";
break;
case "stench_over.gif":
lowerImageName = "stench_over_gif";
break;
case "voc_over.gif":
lowerImageName = "voc_over_gif";
break;
case "wastewater_over.gif":
lowerImageName = "wastewater_over_gif";
break;
case "waterquality_over.gif":
lowerImageName = "waterquality_over_gif";
break;
}
String noImageName = Utils.getFileNameNoEx(lowerImageName);
int idImage = Utils.getImageResourceId(noImageName);
MarkerOptions options = new MarkerOptions()
.position(new LatLng(comPointBean.getOtherInfo().get(j).getLatitude(), comPointBean.getOtherInfo().get(j).getLongitude()))//地图上标记位置的经纬度值
.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
.decodeResource(getResources(), idImage)))
.draggable(false)//点标记是否可拖拽
.anchor(0.5f, 0.5f)
.title("" + i++);
optionsArrayList.add(options);
}
}
ArrayList markers = mapview.getMap().addMarkers(optionsArrayList, false);
}