Google Maps API-多边形的不同地图样式 [英] Google Maps API - different map style for polygon
问题描述
我想要达到类似于此处所示的效果:
I want to achieve the effect similar to what is presented here:
在Android谷歌地图内.多边形应具有与其余地图不同的地图样式(json文件).有办法吗? 我什至会接受拥有两个不同的地图(一个在另一个地图之下)并将多边形从顶部地图中切出.那有可能吗?
inside Android google map. The polygon should have a different map style (json file) than the rest of the map. Is there a way of doing that? I would even accept having two different maps (one under the other) and cutting the polygon out of the top map. Would that be possible?
推荐答案
使用 PolygonOptions.addHole()
方法.您应该为所有地图(从-90到90度和从-180到180度)创建灰色透明多边形,并为每个地形位置创建孔.像这样:
That effect is possible with Polygon.setHoles()
or PolygonOptions.addHole()
methods. You should create gray transparent polygon for all map (from -90 to 90 degrees of latitude and from -180 to 180 of longitude) with holes for each of your terrain location. Something like this:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = MainActivity.class.getSimpleName();
private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mMapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
List<List<LatLng>> holes = new ArrayList<>();
// "hole" for Hyde Park
List<LatLng> hole = new ArrayList<>();
hole.add(new LatLng(51.509869, -0.191208));
hole.add(new LatLng(51.513287, -0.158464));
hole.add(new LatLng(51.505540, -0.151769));
hole.add(new LatLng(51.502178, -0.174471));
hole.add(new LatLng(51.502444, -0.187989));
holes.add(hole);
// "hole" for Regent's Park
hole = new ArrayList<>();
hole.add(new LatLng(51.530226, -0.167685));
hole.add(new LatLng(51.534924, -0.163737));
hole.add(new LatLng(51.537566, -0.151849));
hole.add(new LatLng(51.535964, -0.146914));
hole.add(new LatLng(51.525325, -0.145625));
hole.add(new LatLng(51.523589, -0.155538));
holes.add(hole);
mGoogleMap.addPolygon(createPolygonWithHoles(holes));
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.519454, -0.168869), 14));
}
private static List<LatLng> createBoundsOfEntireMap() {
final float delta = 0.01f;
return new ArrayList<LatLng>() {{
add(new LatLng(90 - delta, -180 + delta));
add(new LatLng(0, -180 + delta));
add(new LatLng(-90 + delta, -180 + delta));
add(new LatLng(-90 + delta, 0));
add(new LatLng(-90 + delta, 180 - delta));
add(new LatLng(0, 180 - delta));
add(new LatLng(90 - delta, 180 - delta));
add(new LatLng(90 - delta, 0));
add(new LatLng(90 - delta, -180 + delta));
}};
}
static PolygonOptions createPolygonWithHoles(List<List<LatLng>> holes) {
PolygonOptions polyOptions = new PolygonOptions()
.fillColor(0x33000000)
.addAll(createBoundsOfEntireMap())
.strokeColor(0xFF000000)
.strokeWidth(5);
for (List<LatLng> hole : holes) {
polyOptions.addHole(hole);
}
return polyOptions;
}
}
您会得到类似的东西:
此外,您还需要位图圆作为多边形顶点的标记,或将其绘制为 Circle
对象.
Also you need bitmap circles as markers for polygon vertexes or draw it as Circle
objects.
更新
对于打孔"情况和昼夜"情况,应将.fillColor(0x33000000)
更改为更暗,例如.fillColor(0xDD000000)
,然后在第一个"多边形上添加带孔的多边形.像这样:
For "hole-in-hole" case and "night-and-day" you should change .fillColor(0x33000000)
to more dark, e.g. .fillColor(0xDD000000)
and just add polygon with hole over "first" polygon. Something like this:
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
List<List<LatLng>> holes = new ArrayList<>();
// "hole" for Hyde Park
List<LatLng> hole = new ArrayList<>();
hole.add(new LatLng(51.509869, -0.191208));
hole.add(new LatLng(51.513287, -0.158464));
hole.add(new LatLng(51.505540, -0.151769));
hole.add(new LatLng(51.502178, -0.174471));
hole.add(new LatLng(51.502444, -0.187989));
holes.add(hole);
// "hole" for Regent's Park
hole = new ArrayList<>();
hole.add(new LatLng(51.530226, -0.167685));
hole.add(new LatLng(51.534924, -0.163737));
hole.add(new LatLng(51.537566, -0.151849));
hole.add(new LatLng(51.535964, -0.146914));
hole.add(new LatLng(51.525325, -0.145625));
hole.add(new LatLng(51.523589, -0.155538));
holes.add(hole);
mGoogleMap.addPolygon(createPolygonWithHoles(holes));
List<LatLng> holesInHolesPoly = new ArrayList<>();
holesInHolesPoly.add(new LatLng(51.508184, -0.177805));
holesInHolesPoly.add(new LatLng(51.509759, -0.164373));
holesInHolesPoly.add(new LatLng(51.504549, -0.162399));
holesInHolesPoly.add(new LatLng(51.503453, -0.177934));
List<LatLng> holesInHolesHole = new ArrayList<>();
holesInHolesHole.add(new LatLng(51.505883, -0.172999));
holesInHolesHole.add(new LatLng(51.507992, -0.171025));
holesInHolesHole.add(new LatLng(51.506308, -0.169738));
hole = new ArrayList<>();
hole.add(new LatLng(51.530226, -0.167685));
PolygonOptions holeInHoles = new PolygonOptions()
.fillColor(0xDD000000)
.addAll(holesInHolesPoly)
.addHole(holesInHolesHole)
.strokeColor(0xFF000000)
.strokeWidth(5);
mGoogleMap.addPolygon(holeInHoles);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.519454, -0.168869), 14));
}
您会得到类似的东西:
这不是解决方案,但确实是一种很好的解决方法.或者,您可以为整个地图设置夜晚"样式,并使用自定义 GroundOverlay
和天"位图.无论如何-似乎没有正常"方式为地图的多边形部分创建自定义样式.
It's not a solution, but really good workaround. Or you can set "night" style for whole map and use custom TileProvider
or GroundOverlay
with "day" bitmap. Anyway - seems there are no "normal" way to create custom style for polygon part of the map.
这篇关于Google Maps API-多边形的不同地图样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!