知道经纬度怎么在地图上标记各点代码实现
时间: 2023-06-15 18:08:00 浏览: 62
在地图上标记各点,可以使用各种地图API实现,比如Google Maps API、百度地图API等。以下是使用Google Maps API在网页上标记经纬度点的示例代码:
1. 在html文件中引入Google Maps API:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Google Maps API Demo</title>
<style>
#map {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<h1>Google Maps API Demo</h1>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
<script src="app.js"></script>
</body>
</html>
```
请将YOUR_API_KEY替换为您的Google Maps API密钥。
2. 在JavaScript文件中编写代码:
```js
// 初始化地图
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8, // 缩放级别
center: {lat: 39.9042, lng: 116.4074} // 中心点坐标
});
// 在地图上标记经纬度点
var marker = new google.maps.Marker({
position: {lat: 39.9042, lng: 116.4074}, // 经纬度坐标
map: map,
title: '北京' // 标题
});
}
```
以上代码将在地图上标记北京的经纬度点,并在鼠标悬停时显示标题“北京”。
当然,您可以通过编写更多JavaScript代码,使用循环或其他方法在地图上标记多个经纬度点。