- Google 地图教程
- Google 地图 - 首页
- Google 地图 - 开始
- Google 地图 - 类型
- Google 地图 - 缩放
- Google 地图 - 本地化
- Google 地图 - UI 控件
- Google 地图 - 标记
- Google 地图 - 图形
- Google 地图 - 信息窗口
- Google 地图 - 符号
- Google 地图 - 事件
- Google 地图资源
- Google 地图 - 快速指南
- Google 地图 - 有用资源
- Google 地图 - 讨论
Google 地图 - 图形
在上一章中,我们学习了如何在 Google 地图中使用标记。除了标记之外,我们还可以添加各种形状,例如圆形、多边形、矩形、多段线等。本章解释如何使用 Google 地图提供的形状。
多段线
Google 地图提供的多段线可用于追踪不同的路径。您可以通过实例化类`google.maps.Polyline`来向地图添加多段线。实例化此类时,我们必须指定多段线属性的所需值,例如 StrokeColor、StokeOpacity 和 strokeWeight。
我们可以通过将其对象传递给`setMap(MapObject)`方法来向地图添加多段线。我们可以通过向 SetMap() 方法传递空值来删除多段线。
示例
以下示例显示了一条多段线,突出显示了德里、伦敦、纽约和北京之间的路径。
<!DOCTYPE html>
<html>
<head>
<script src = "https://maps.googleapis.com/maps/api/js"></script>
<script>
function loadMap(){
var mapProp = {
center:new google.maps.LatLng(51.508742,-0.120850),
zoom:3,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var tourplan = new google.maps.Polyline({
path:[
new google.maps.LatLng(28.613939, 77.209021),
new google.maps.LatLng(51.507351, -0.127758),
new google.maps.LatLng(40.712784, -74.005941),
new google.maps.LatLng(28.213545, 94.868713)
],
strokeColor:"#0000FF",
strokeOpacity:0.6,
strokeWeight:2
});
tourplan.setMap(map);
//to remove plylines
//tourplan.setmap(null);
}
</script>
</head>
<body onload = "loadMap()">
<div id = "googleMap" style = "width:580px; height:400px;"></div>
</body>
</html>
它将产生以下输出:
多边形
多边形用于突出显示州或国家/地区的特定地理区域。您可以通过实例化类`google.maps.Polygon`来创建所需的多边形。实例化时,我们必须为多边形的属性指定所需的值,例如 path、strokeColor、strokeOapacity、fillColor、fillOapacity 等。
示例
以下示例显示了如何绘制多边形以突出显示海得拉巴、纳格浦尔和奥兰加巴德等城市。
<!DOCTYPE html>
<html>
<head>
<script src = "https://maps.googleapis.com/maps/api/js"></script>
<script>
function loadMap(){
var mapProp = {
center:new google.maps.LatLng(17.433053, 78.412172),
zoom:4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myTrip = [
new google.maps.LatLng(17.385044, 78.486671),
new google.maps.LatLng(19.696888, 75.322451),
new google.maps.LatLng(21.056296, 79.057803),
new google.maps.LatLng(17.385044, 78.486671)
];
var flightPath = new google.maps.Polygon({
path:myTrip,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2,
fillColor:"#0000FF",
fillOpacity:0.4
});
flightPath.setMap(map);
}
</script>
</head>
<body onload = "loadMap()">
<div id = "googleMap" style = "width:580px; height:400px;"></div>
</body>
</html>
它将产生以下输出:
矩形
我们可以使用矩形使用矩形框来突出显示特定区域或州的地理区域。我们可以通过实例化类`google.maps.Rectangle`在地图上添加矩形。实例化时,我们必须为矩形的属性指定所需的值,例如 path、strokeColor、strokeOapacity、fillColor、fillOapacity、strokeWeight、bounds 等。
示例
以下示例显示了如何使用矩形突出显示地图上的特定区域:
<!DOCTYPE html>
<html>
<head>
<script src = "https://maps.googleapis.com/maps/api/js"></script>
<script>
function loadMap(){
var mapProp = {
center:new google.maps.LatLng(17.433053, 78.412172),
zoom:6,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myrectangle = new google.maps.Rectangle({
strokeColor:"#0000FF",
strokeOpacity:0.6,
strokeWeight:2,
fillColor:"#0000FF",
fillOpacity:0.4,
map:map,
bounds:new google.maps.LatLngBounds(
new google.maps.LatLng(17.342761, 78.552432),
new google.maps.LatLng(16.396553, 80.727725)
)
});
}
</script>
</head>
<body onload = "loadMap()">
<div id = "googleMap" style = "width:580px; height:400px;"></div>
</body>
</html>
这将提供以下输出:
圆形
与矩形一样,我们可以使用圆形来使用圆形突出显示特定区域或州的地理区域,方法是实例化类`google.maps.Circle`。实例化时,我们必须为圆形的属性指定所需的值,例如 path、strokeColor、strokeOapacity、fillColor、fillOapacity、strokeWeight、radius 等。
示例
以下示例显示了如何使用圆形突出显示新德里周围的区域:
<!DOCTYPE html>
<html>
<head>
<script src = "https://maps.googleapis.com/maps/api/js"></script>
<script>
function loadMap(){
var mapProp = {
center:new google.maps.LatLng(28.613939,77.209021),
zoom:5,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myCity = new google.maps.Circle({
center:new google.maps.LatLng(28.613939,77.209021),
radius:150600,
strokeColor:"#B40404",
strokeOpacity:0.6,
strokeWeight:2,
fillColor:"#B40404",
fillOpacity:0.6
});
myCity.setMap(map);
}
</script>
</head>
<body onload = "loadMap()">
<div id = "googleMap" style = "width:580px; height:400px;"></div>
</body>
</html>
它将产生以下输出: