Home  >  Article  >  Web Front-end  >  How to solve the problem that uniapp Tencent map cannot resolve the address

How to solve the problem that uniapp Tencent map cannot resolve the address

PHPz
PHPzOriginal
2023-04-17 11:30:241202browse

As a uniapp developer, you may encounter some problems when using Tencent Map SDK. One of the common problems is that the address cannot be resolved. This problem will cause the map to be unable to display address markers during development, affecting the user experience. This article will introduce some ways to solve this problem.

Problem description:

When using Tencent Map SDK, the address entered by the user cannot be marked on the map, and only a default mark is displayed on the map.

Cause analysis:

Tencent Map SDK has certain restrictions on parsing addresses, and the address needs to be entered in a certain format. A common situation is that the address is not formatted, resulting in parsing failure.

Solution:

  1. Format address

When using Tencent Map SDK, the address input by the user should be formatted to ensure that the address The format is correct. You can use a third-party address formatting library, such as the "address-parse" library, which can parse the address entered by the user into a standard format to improve the accuracy of address parsing. The code is as follows:

import address from 'address-parse';

let data = address('广东省深圳市南山区学府路XXXX号');
console.log(data);

The output result is:

{
  province: '广东省',
  city: '深圳市',
  district: '南山区',
  address: '学府路XXXX号'
}
  1. Use geocoding interface

If you still cannot parse the address after formatting, you can try to use "Geocoding interface" provided by Tencent Maps. The geocoding interface can parse addresses into latitude and longitude, and then mark the latitude and longitude on the map. The code is as follows:

import QQMapWX from 'wx-jssdk';

let qqmapsdk = new QQMapWX({
  key: '你的腾讯地图APIKEY'
});

// 地理编码
qqmapsdk.geocoder({
  address: '广东省深圳市南山区学府路XXXX号',
  success: function(res) {
    console.log(res);
  },
  fail: function(res) {
    console.log(res);
  }
});

The output result is:

{
  status: 0,
  message: 'query ok',
  result: {
    location: {
      lat: 22.540285,
      lng: 113.950587
    },
    address: '广东省深圳市南山区XXXXX',
    formatted_addresses: { recommend: '广东省深圳市南山区XXXXX' },
    ad_info: {
      adcode: '440305',
      nation_code: '156',
      city_code: '0755',
      name: '深圳市',
      location: { lat: 22.542983, lng: 114.059849 },
      nation: '中国'
    }
  }
}
  1. Update SDK version

If neither of the above two methods can solve the problem, you can try to update Tencent Map SDK version. The updated version of Tencent Map SDK may fix some address parsing problems.

Conclusion:

Resolving addresses is one of the foundations of using Tencent Map SDK. When you encounter an address that cannot be resolved during development, you should deal with it in time to improve the accuracy of address resolution. Most geocoding issues can be solved by formatting the address, using the geocoding interface, and updating the SDK version.

The above is the detailed content of How to solve the problem that uniapp Tencent map cannot resolve the address. 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