Home > Web Front-end > JS Tutorial > How to use JS and Amap to implement location marking function

How to use JS and Amap to implement location marking function

PHPz
Release: 2023-11-21 13:26:06
Original
1549 people have browsed it

How to use JS and Amap to implement location marking function

How to use JS and Amap to implement the location marking function

The location marking function is a common requirement in web applications. By marking specific locations on the map Location, convenient for users to view and locate. This article will introduce how to use JavaScript and Amap API to implement this function, and provide specific code examples.

Amap is a leading map service provider in China, and its API provides a wealth of map-related functions. To implement the location marking function, we first need to introduce the JavaScript file of the Amap API into the HTML page. The sample code is as follows:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>地点标记示例</title>
  <style>
    #map {
      width: 100%;
      height: 600px;
    }
  </style>
</head>
<body>
  <div id="map"></div>
  <script src="https://webapi.amap.com/maps?v=1.4.15&key=您的高德地图API密钥"></script>
  <script src="https://webapi.amap.com/ui/1.0/main.js"></script>
  <script src="script.js"></script>
</body>
</html>
Copy after login

In the above code, we introduced it through the script tag JavaScript file of Amap API. The key parameter needs to be replaced with your own Amap API key, otherwise the map function will not work properly.

Next, we need to write JavaScript code to implement the location marking function. In the script.js file, we can follow the following steps:

  1. Create a map container: Use the AMap.Map() function to create a map container , and specify the container's ID and initial configuration as follows:
var map = new AMap.Map('map', {
  center: [116.397428, 39.90923], // 地图中心点的经纬度坐标
  zoom: 13 // 地图缩放级别
});
Copy after login
  1. Add a location marker: Use the AMap.Marker() function to create a location marker, and Specify the position and other attributes of the mark, as shown below:
var marker = new AMap.Marker({
  position: [116.397428, 39.90923], // 标记位置的经纬度坐标
  title: '北京市', // 标记的名称
  map: map // 标记所属的地图对象
});
Copy after login
  1. Set the style of the location mark: You can pass setIcon() of the AMap.Marker class Method to set the icon style of the marker as follows:
marker.setIcon('https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png');
Copy after login

The above code will use the blue marker icon provided by Amap as the style of the location marker.

  1. Add information form: You can create an information form through the AMap.InfoWindow class to display detailed information about the location. The sample code is as follows:
var infoWindow = new AMap.InfoWindow({
  content: '这是北京市的详细信息', // 信息窗体的内容
  offset: new AMap.Pixel(0, -30) // 信息窗体相对于地点标记的偏移量
});

// 给地点标记添加点击事件,点击标记时显示信息窗体
marker.on('click', function() {
  infoWindow.open(map, marker.getPosition());
});
Copy after login

In the above code, we first create an information form, and then add a click event for the location marker. When the user clicks the marker, the information form will be displayed in on the marked position.

Through the above steps, we can add location markers on the map and realize the function of clicking to display detailed information. The complete sample code is as follows:

var map = new AMap.Map('map', {
  center: [116.397428, 39.90923],
  zoom: 13
});

var marker = new AMap.Marker({
  position: [116.397428, 39.90923],
  title: '北京市',
  map: map
});

marker.setIcon('https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png');

var infoWindow = new AMap.InfoWindow({
  content: '这是北京市的详细信息',
  offset: new AMap.Pixel(0, -30)
});

marker.on('click', function() {
  infoWindow.open(map, marker.getPosition());
});
Copy after login

Save the above code as a script.js file and run it together with the previous HTML code, you will see a map with location markers, And when the mark is clicked, an information form with detailed information will be displayed.

In actual applications, you can adjust the center point of the map, zoom level, marker position and style, as well as the content and style of the information form according to specific needs, thereby realizing customized location marking functions. .

The above is the detailed content of How to use JS and Amap to implement location marking function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template