Home  >  Article  >  Web Front-end  >  Using JavaScript and Tencent Maps to implement map interactive navigation function

Using JavaScript and Tencent Maps to implement map interactive navigation function

WBOY
WBOYOriginal
2023-11-21 14:33:421167browse

Using JavaScript and Tencent Maps to implement map interactive navigation function

Title: Using JavaScript and Tencent Maps to implement map interactive navigation function

The navigation function plays an important role in modern society, it can help people more Get to know a place well, find your destination and plan your route. This article will introduce how to use JavaScript and Tencent Map API to implement map interactive navigation function, and provide specific code examples.

1. Preparation work
Before we start writing code, we need to do some preparation work:

  1. Obtain the developer key of Tencent Map, which can be opened on Tencent Map Platform application.
  2. Create an HTML file and introduce Tencent Map’s JavaScript library:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>地图导览</title>
      <script src="https://map.qq.com/api/js?v=2.exp&key=YOUR_KEY"></script>
    </head>
    <body>
      <div id="map" style="width: 100%; height: 500px;"></div>
    </body>
    </html>

    Please replace YOUR_KEY above with your Tencent Map developer key.

2. Display the map
Next, we will use JavaScript to display a map on the web page. Add the following JS code within the

tag of the HTML file:
<script>
  var map = new qq.maps.Map(document.getElementById("map"), {
    center: new qq.maps.LatLng(39.908722, 116.397499), // 地图中心点坐标
    zoom: 13 // 地图缩放级别
  });
</script>

This code creates a map instance and sets the map's center point coordinates and zoom level.

3. Add marker points and information window
Now, we can add marker points on the map and set an information window for each marker point. This window will pop up when the user clicks on the marker point.

First, we need to define the location of a marker point and the content of an information window. In the JS code, add the following code:

<script>
  var marker = new qq.maps.Marker({
    position: new qq.maps.LatLng(39.908722, 116.397499),
    map: map
  });
  var infoWindow = new qq.maps.InfoWindow({
    content: "这是一个标记点的信息窗口!"
  });
  qq.maps.event.addListener(marker, 'click', function() {
    infoWindow.open();
  });
</script>

This code creates a marker point and an information window and adds them to the map. When the user clicks on the marker point, an information window will pop up to display the defined content.

4. Add navigation function
Now, we can provide users with navigation function, allowing them to plan a route from the current location to the target location by clicking on the marked point on the map.

First, we need to get the user's current location. In the JS code, add the following code:

<script>
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var currentPosition = new qq.maps.LatLng(position.coords.latitude, position.coords.longitude);
      var currentMarker = new qq.maps.Marker({
        position: currentPosition,
        map: map
      });
      var drivingService = new qq.maps.DrivingService({
        map: map
      });
      qq.maps.event.addListener(marker, 'click', function() {
        drivingService.search(currentPosition, marker.getPosition());
      });
    });
  }
</script>

This code uses HTML5’s Geolocation API to obtain the user’s current location and add a marker on the map that represents the current location. At the same time, we also created a DrivingService object for route planning, and called the drivingService.search() method to plan the route when the marker point is clicked.

The above is a specific code example that uses JavaScript and Tencent Map API to implement the interactive map navigation function. Through these codes, we can display the map on the web page, add markers and information windows, and provide navigation functions for users. I hope this article can help you understand and use Tencent Map API!

The above is the detailed content of Using JavaScript and Tencent Maps to implement map interactive navigation 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