26
2017
09

android百度地图行政区填充颜色

android百度地图行政区填充颜色

给行政区填充颜色其实就是添加几何图形覆盖物,需要行政区边界上面的点的集合这样可以添加几何图形覆盖物,原理就是这样的。这是百度地图添加几何图形覆盖物网址
——http://lbsyun.baidu.com/index.php?title=androidsdk/guide/basicmap。效果如下:
这里写图片描述
下面介绍具体实现方法:

    public class DistrictSearchDemo extends Activity implements OnGetDistricSearchResultListener, Button.OnClickListener {

    private DistrictSearch mDistrictSearch;
    private EditText mCity;
    private EditText mDistrict;
    private MapView mMapView;
    private final int color = 0xAA00FF00;
    private BaiduMap mBaiduMap;
    private Button mSearchButton;
    String[] address={"苏州","上海","南京","无锡","徐州","常州"};//行政区集合
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district_search_demo);
        mDistrictSearch = DistrictSearch.newInstance();
        mDistrictSearch.setOnDistrictSearchListener(this);

        mMapView = (MapView) findViewById(R.id.map);
        mBaiduMap = mMapView.getMap();
        mCity = (EditText) findViewById(R.id.city);
        mDistrict = (EditText) findViewById(R.id.district);
        mSearchButton = (Button) findViewById(R.id.districSearch);
        mSearchButton.setOnClickListener(this);
    }

    @Override
    public void onGetDistrictResult(DistrictResult districtResult) {
        if (districtResult == null) {
            return;
        }
        if (districtResult.error == SearchResult.ERRORNO.NO_ERROR) {
            List<List<LatLng>> polyLines = districtResult.getPolylines();
            Toast.makeText(DistrictSearchDemo.this, districtResult.getCityName(), Toast.LENGTH_SHORT).show();
            if (polyLines == null) {
                return;
            }
            // LatLngBounds.Builder builder = new LatLngBounds.Builder();
            for (List<LatLng> polyline : polyLines) {
                OverlayOptions ooPolygon = new PolygonOptions().points(polyline).stroke(new Stroke(5, 0xAA00FF88))
                        .fillColor(0xAAFFFF00);
                mBaiduMap.addOverlay(ooPolygon);
            }
            if (aa<address.length) {
                mDistrictSearch.searchDistrict(new DistrictSearchOption().cityName(city).districtName(address[aa]));
                aa++;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mDistrictSearch.destroy();
        super.onDestroy();
    }

    int aa = 0;
String city = "";
    @Override
    public void onClick(View v) {
        String district = "";
        if (mCity.getText() != null && !"".equals(mCity.getText())) {
            city = mCity.getText().toString();
        }
        if (mDistrict.getText() != null && !"".equals(mDistrict.getText())) {
            district = mDistrict.getText().toString();
        }
        if (aa == 0) {
            mDistrictSearch.searchDistrict(new DistrictSearchOption().cityName(city).districtName("苏州"));//获取行政区点集合方法
            aa = 1;
        }

    }
}

最后点击开始就可以实现,要滑动到显示的城市范围才能看到,效果图如下:
这里写图片描述
最后附上demo链接,
如有问题可以评论指出,稍后更新demo链接地址。

上一篇:对 VirtualApp hook过程的理解 下一篇:Android 开发,工作中常用的软件