Detailed explanation of the implementation of WeChat applet development to locate the current city code

Source: Internet
Author: User
This article mainly introduces information about how small programs locate the current city to implement instance code, for more information about how to locate the instance code in the current city, see this article.

The applet locates in the current city

First, you need to apply for the Baidu map Geocoding API

Geocoding APIs include Address Resolution and Reverse Address Resolution:

1. geographic code: Address Resolution. Baidu's latitude and longitude information is obtained from the structured address of the street in detail. for example, the Address Resolution result of "27 Zhongguancun South Street, Haidian district, Beijing" is "lng: 116.31985, lat: 39.959836 ". At the same time, geocoding also supports direct resolution of scenic sites and landmarks and returns Baidu longitude and latitude. for example, the resolution result of "Baidu Tower" address is "lng: 116.30815, lat: 40.056885 ", for general POI search requirements, we recommend that you use the Place API.

2. inverse geographic code: This is Reverse Address Resolution. structured address information is obtained from Baidu's latitude and longitude information, for example, "lat: 31.325152, lng: 120.558957 "the result of Reverse Address Resolution is" No. 318, Tayuan Road, Huqiu district, Suzhou city, Jiangsu province ".

Code:


Page ({data: {city: ''}, onLoad: function (options) {this. loadInfo () ;}, loadInfo: function () {var page = this wx. getLocation ({type: 'wgs84 ', // The default value is wgs84, which returns the gps coordinates. gcj02 returns the coordinates that can be used for wx. coordinates of openLocation success: function (res) {// success var long1_= res. long1_var latitude = res. latitude page. loadCity (longbench, latitude)}, fail: function () {// fail}, complete: function () {// complete})}, loadCity: Function (longpolling, latitude) {var page = this wx. request ({url: 'https: // api.map.baidu.com/geocoder/v2 /? Ak = your ak & location = '+ latitude +', '+ longpolling +' & output = json', data: {}, header: {'content-type ': 'application/json'}, success: function (res) {// success console. log (res); var city = res. data. result. addressComponent. city; page. setData ({city: city}) ;}, fail: function () {// fail}, complete: function () {// complete }})}})

Index. wxml


  
 
   {{city}} 
 

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

The above is a detailed description of the implementation of mini-program development to locate the details of the current city code, please pay attention to other articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.