google maps android v2开发基础(四)

      Polyline定义了一组组连的线段在地图上,一个折线对象由一组经纬度位置组成,并按一个有序的序列建立一系列线段连接起这些地点。要创建一个Polyline首先要创建一个PolylineOptions的 对象,点与点之间的连线顺序将它们添加到PolylineOptions,要添加点到PolylineOptions调用PolylineOptions.add()。 GoogleMap.addPolyline(PolylineOptions)把PolylineOptions添加到GoogleMap里。以下实现添加矩形地图:
// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);
      Polygons一定条件下是跟Polyline相等的,你可以根据上面添加Polyline到GoogleMap的方法添加一个Polygons到GoolgMap,以下代码添加一个多边形到GoogleMap中
// Instantiates a new Polygon object and adds points to define a rectangle
PolygonOptions rectOptions = new PolygonOptions()
              .add(new LatLng(37.35, -122.0),
                   new LatLng(37.45, -122.0),
                   new LatLng(37.45, -122.2),
                   new LatLng(37.35, -122.2),
                   new LatLng(37.35, -122.0));

// Get back the mutable Polygon
Polygon polygon = myMap.addPolygon(rectOptions);

你可以调用 Polygon.setPoints()方法,并且提供一系列的经纬度以改变一个已添加的Polygons。

      Polygons的自动关闭,上面的例子Polygons共有五个坐标,而且你会发现第一个坐标跟最后一个坐标是相同的,实现情况下Polygons定义了自动关闭区域,并不需要定义最后的坐标。以下两段代码是等效的:

Polygon polygon = map.addPolygon(new PolygonOptions()
        .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0))
        .strokeColor(Color.RED)
        .fillColor(Color.BLUE));

Polygon polygon = map.addPolygon(new PolygonOptions()
         .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5))
         .strokeColor(Color.RED)
         .fillColor(Color.BLUE));
      自定义Polyline Polygons外观,你要在Polyline Polygons添加到GoolgeMap之前设置好它们的外观属性:
Polyline line = map.addPolyline(new PolylineOptions()
    .add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734))
    .width(25)
    .color(Color.BLUE)
    .geodesic(true));
      创建一个空心的Polygons,两个路径必须定义在相同的区域内,大的区域除去小区域部分为空心的Polygons,通过addHole()方法添加空心部分的区域。
List<LatLng> latLngs = new ArrayList<LatLng>();
		latLngs.add(new LatLng(37.45, -120.0));
		latLngs.add(new LatLng(37.65, -122.5));
		latLngs.add(new LatLng(37.0, -123.0));
		latLngs.add(new LatLng(37.05, -122));
		latLngs.add(new LatLng(37.45, -120.0));
		
		mMap.addPolygon(new PolygonOptions()
		.add(new LatLng(37.35, -122.0), new LatLng(37.45, -122.0), new LatLng(37.45, -122.2), new LatLng(37.35, -122.2), new LatLng(37.35, -122.0))
        .addHole(latLngs)
        .fillColor(Color.BLUE));




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值