“This is the third day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Autonavi Open Platform

Tune autonavi interface what we need the most important ❓ need autonavi map key. Follow the steps below to access the Autonavi open platform.

Create an

Add the key

Notice here that different service platforms correspond to different available services. I am using a Web service as shown below

To generate the key

Koa requests a third-party interface

koa2-request

Making a request to a third-party interface in Node is essentially making a request. The same is true of reptiles. Koa2-request is used here. Because we’re using the KOA framework.

  • 📮 address: www.npmjs.com/package/koa… Actually, you don’t need to read it. That’s all.

  • 🪛 installation:

npm install koa2-request
Copy the code
  • ⚙ Basic usage

Async await is supported here

var koa2Req = require('koa2-request');
 
app.use(async(ctx, next) => {
    / / request option
    var res = await koa2Req('http://www.baidu.com');
    ctx.body = res.body;
});
Copy the code

Open dry

The interface

We came in and were surprised to find that he needed city and key as parametersHowever, it is not practical for us to manually enter the code corresponding to the city. Even if I could remember, it would be a terrible user experience. And actually Autonavi also has an IP location interface. So let’s jump down here.

IP position

🗺 lbs.amap.com/api/webserv…

Two parameters are required: IP and key

Speaking of IP, you need a plugin

  • 📪 address www.npmjs.com/package/pub…
  • ⚙ Basic usage

const publicIp = require('public-ip');

(async() = > {console.log(await publicIp.v4());
	/ / = > '46.5.21.123'

	console.log(await publicIp.v6());
	//=> 'fe80::200:f8ff:fe21:67cf'}) ();Copy the code

Here is my implementation, taking IP and key as parameters

const koa2Req = require('koa2-request');
const publicIp = require('public-ip') // Obtain the external IP address
const gaode_key = '8a674879652195a8bc6ff51357199517'
class clientController {
    async getWeather(ctx, next) {
        const ip_param = await publicIp.v4()
        var res = await koa2Req(`https://restapi.amap.com/v3/ip?ip=${ip_param}&output=json&key=${gaode_key}`); ctx.body = res; }}Copy the code

The format of the return value

{

       "status" :"1"."info" :"OK"."infocode" :"10000"."province" :"Beijing"."city" :"Beijing"."adcode" :"110000"."rectangle" :"116.0119343, 39.66127144; 116.7829835, 40.2164962,"

  }
Copy the code

We want to get the city code adcode, res.body is the value we get back from the interface. Parse it into a JSON object with json.parse.


    async getWeather(ctx, next) {
        const ip_param = await publicIp.v4()
        var res = await koa2Req(`https://restapi.amap.com/v3/ip?ip=${ip_param}&output=json&key=${gaode_key}`);
        const city = JSON.parse(res.body).adcode
        console.log(city,'city')}Copy the code

Next, we can call the weather interface, which takes the city code and key we just obtained as parameters.


    async getWeather(ctx, next) {
        const ip_param = await publicIp.v4()
        var res = await koa2Req(`https://restapi.amap.com/v3/ip?ip=${ip_param}&output=json&key=${gaode_key}`);
        const city = JSON.parse(res.body).adcode
        console.log(city,'city')
        var res_weather = await koa2Req(`https://restapi.amap.com/v3/weather/weatherInfo?city=${city}&key=${gaode_key}`)
        let weather = {data: JSON.parse(res_weather.body)}
        ctx.body = weather;
    }
Copy the code