search
HomeWeb Front-endJS TutorialHow to use JS and Amap to implement location marking function

How to use JS and Amap to implement location marking function

Nov 21, 2023 pm 01:26 PM
jsGaode mapPlace markers

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>

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 // 地图缩放级别
});
  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 // 标记所属的地图对象
});
  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');

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());
});

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());
});

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!

Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software