Home Web Front-end JS Tutorial How to use JS and Amap to implement the search function around a location

How to use JS and Amap to implement the search function around a location

Nov 21, 2023 am 08:26 AM
js Gaode map Search around location

How to use JS and Amap to implement the search function around a location

How to use JS and Amap to implement location search function

Introduction:
In today's digital society, many applications need to use maps to provide Positioning and navigation functions. Amap is one of the most well-known and powerful map service providers in China. This article will introduce how to use JS and Amap API to implement the location search function, and provide specific code examples.

1. Preparation

  1. Obtain an Amap developer account
    Before starting, you need to register an Amap developer account and obtain a developer key ( key). Through this key, we can use the API functions of Amap.
  2. Introduce the Amap API
    Introduce the JS library file of Amap into the page, the code is as follows:

    <script src="https://webapi.amap.com/maps?v=1.4.15&key=yourKey"></script>
    Copy after login

    Among them, yourKey is you developer key.

2. Implement the search function around the location
Next, we will use JS and Amap API to implement the search function around the location.

  1. Create a map instance
    First, create a container in the page to display the map. The code is as follows:

    <div id="map"></div>
    Copy after login

    Then, create a map instance in the JS code, the code is as follows:

    var map = new AMap.Map('map', {
     zoom: 14, // 设置地图缩放级别
     center: [116.397428, 39.90923] // 设置地图中心点坐标
    });
    Copy after login

    In the above code, we set the zoom level of the map to 14, and set the map's The coordinates of the center point are [116.397428, 39.90923] (the coordinates of Beijing City).

  2. Add location mark
    Before implementing the search function around the location, we need to add a mark on the map to represent the current location. The code is as follows:

    var marker = new AMap.Marker({
     position: [116.397428, 39.90923], // 设置标记的位置坐标
     map: map // 将标记添加到地图上
    });
    Copy after login

    In the above code, we set the marker's position coordinates to [116.397428, 39.90923] and added the marker to the previously created map instance.

  3. Realize search around the location
    Next, we will use the POI search function of Amap to implement search around the location. The code is as follows:

    // 创建一个POI搜索对象
    var placeSearch = new AMap.PlaceSearch({
     pageSize: 10, // 每页显示的结果数量
     pageIndex: 1, // 当前页码
     city: '北京', // 设置搜索的城市
     citylimit: true // 限制搜索结果范围在当前城市
    });
    
    // 执行搜索
    placeSearch.searchNearBy('餐馆', [116.397428, 39.90923], 1000, function(status, result) {
     if (status === 'complete' && result.info === 'OK') {
         // 处理搜索结果
         var pois = result.poiList.pois;
         for (var i = 0; i < pois.length; i++) {
             var poi = pois[i];
             console.log(poi.name);
             console.log(poi.address);
         }
     } else {
         console.log('搜索失败');
     }
    });
    Copy after login

    In the above code, we create a POI search object and set the number of results displayed on each page, the current page number, the city searched, and the result range is limited to the current city. Then, we call the searchNearBy method to perform the search, where the parameter 'restaurant' represents the search keyword, [116.397428, 39.90923] represents the center coordinate of the search, 1000 indicates the search radius range (unit is meters).

When the search is completed, the search results are processed through the callback function. We can obtain the name and address information of each location from the results and process it further.

Conclusion:
By using JS and Amap API, we can easily implement the search function around the location. In practical applications, parameters such as search keywords, search center coordinates, and search range can be adjusted according to needs to meet specific business needs. I hope this article can help you understand how to use JS and Amap API to implement the search function around the location.

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

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to register for Amap How to register for Amap Apr 08, 2024 pm 04:39 PM

1. First install and open the Amap app on your mobile phone, click [My] and select [Login/Register]. 2. Select a mobile phone number, WeChat or Alipay to register as needed, and fill in personal information according to the prompts, including mobile phone number, password, etc. 3. After completing the filling, click [Register] to complete the account registration. 4. Afterwards, use the method selected during registration for login verification. If you register through a mobile phone number, you need to enter your mobile phone number and password to log in.

Does Amap require mobile phone registration? Does Amap require mobile phone registration? May 05, 2024 pm 05:12 PM

Yes, for security, personalized services and account management, Amap requires registration with a mobile phone number. The registration steps include: Open the Amap app, click "My" and "Login/Register", select a mobile phone number to register, enter the mobile phone number to get the verification code, set a password to complete the registration.

Operation steps for car invoicing on Amap Operation steps for car invoicing on Amap Apr 01, 2024 pm 10:10 PM

1. First open the Amap and click [Route]. 2. Click [Call a Car] and click [Personal Center] on the left. 3. Click [Invoice]. 4. Check the itinerary and click [Invoice].

Why is there no sound in the Amap navigation? Why is there no sound in the Amap navigation? Apr 02, 2024 am 05:09 AM

The reasons why there is no sound in Amap navigation include improper speaker connection, lowering the device volume, incorrect Amap settings, background application interference, mobile phone silent or vibration mode, and system permission issues. The solutions are as follows: check the speaker connection; adjust the volume; check the Amap map settings; close background applications; check the phone mode; grant permissions; restart the device; update the Amap map; and contact customer service.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Xiaomi CarWith joins hands with Amap to open a new era of lane-level navigation Xiaomi CarWith joins hands with Amap to open a new era of lane-level navigation Apr 16, 2024 pm 08:34 PM

According to news on April 16, Xiaomi users have recently welcomed a practical new feature - Xiaomi CarWith has officially launched Amap lane navigation. The launch of this service will undoubtedly bring drivers a more accurate and convenient navigation experience. According to the data, the integration of Amap and CarWith has achieved seamless connection, and users can directly experience the precise guidance of lane-level navigation without the need for additional software updates. This improvement is likely to be made on the server side, saving users the tedious update step. Car lane-level navigation is an innovative feature of Amap. It can restore the real road layout to a high degree on the screen, clearly displaying the number of lanes, ground markings, entrances and exits, special lanes and other information on the current road, providing drivers with a more comprehensive ,

How to view travel records on Amap How to view travel records on Amap May 05, 2024 pm 05:21 PM

Steps to view travel records on Amap: 1. Log in to Amap; 2. Enter "My" → "My Travel"; 3. View the list of travel records; 4. Click to view details; 5. Export records (optional) .

How to use Amap to open family maps and share maps with family members. Methods and steps for sharing maps with family members. How to use Amap to open family maps and share maps with family members. Methods and steps for sharing maps with family members. May 04, 2024 pm 03:34 PM

Amap APP is a professional and easy-to-use free map navigation software. Everyone likes it very much. It has a variety of functions, which can bring great convenience to our lives. What? Inquiries about some locations, planning of routes, viewing some street view maps, or inquiries about longitude and latitude can be solved here. The operation is simple and convenient, beyond your imagination. Many times, everyone likes it. Sharing maps and location information here makes people feel more secure, which is very good. Many times, for some children or elderly people at home, it will make people more worried when they go out. , when encountering various situations, you can avoid the situation where everyone will get lost.

See all articles