Implementation steps
1. Query interface
There are quite a few interfaces of this type on the website. The author directly found the interface of Baidu Maps to do it. The API called is a geocoding service in the Geocoding API.
Request example: Geocode query of Baidu Building in Beijing
http://api.map.baidu.com/geocoder/v2/?ak=E4805d16520de693a3fe707cdc962045&callback=renderOption&output=json&address=百度大厦&city=北京市
This requires an ak parameter, which is a string generated by the user when creating an application and needs to be called when requesting data.
[Notice]
The created application is server-side type
There are two verification methods to choose from when creating an application. You can choose to use IP whitelist verification or use SN for verification. The difference between the two is that the IP needs to set the IP address when you request it in advance. If you do not want to set the dead IP address in advance, you can also choose SN verification. This is the verification method using md5 as the encryption algorithm.
The author chose SN for verification at the beginning, but called crypto to generate md5 signatures and kept verifying it. He could only use the IP whitelist as verification instead.
2. Nodejs query
With an interface for call, we can write a small script to request data. We need three dependencies, namely express, superagent, and eventproxy
Express is a lightweight web application
Superagent is a library that is often used by crawlers, which can simulate various requests.
eventproxy is a concurrent controller
* Simple query
First, let's write a simple request to detect whether the geographical location can be obtained:
app.get('/one', function(req, res, next) { var sk = 'yoursk' // Create the application's sk, address = 'Beijing' ; superagent.get('http://api.map.baidu.com/geocoder/v2/') .query({address: address}) .query({output: 'json'}) .query({ak: sk}) .end(function(err, sres) { if (err) { console.log('err:', err); return; } res.send(sres.text); }) }) Then open the browser to visit: http://localhost:8888/one
{ status: 0, result: { location: { lng: 116.39564503787867, lat: 39.92998577808024 }, precision: 0, confidence: 10, level: "city"}When you can see this information, it means that the interface is successful. If the status is not 0, please refer to the return code status table
Why do we need to open a server to request it? Because the application we created is a server, we need to build a server to request it.
* Batch query
Okay, a city can be queried. Next we will conduct querying multiple cities. We use eventproxy for concurrency control. You can think of it as a counter. You can command it to listen for an event and execute the corresponding function n times.
The key code is as follows:
app.get('/many', function(req, res, next) { var sk = 'yoursk' , addresses = ['Beijing', 'Shenzhen', 'Guangzhou', 'Puning'] ; ep.after('getLocation', addresses.length, function(locations) { res.send(locations); }) addresses.forEach(function(e, i) { superagent.get('http://api.map.baidu.com/geocoder/v2/') .query({address: e}) .query({output: 'json'}) .query({ak: sk}) .end(function(err, sres) { ep.emit('getLocation', {address: e, res: sres.text}) }) }) }) }) Open the browser to access: http://localhost:8888/many
[{address: "Beijing",res: "{"status":0,"result":{"location":{"lng":116.39564503787867,"lat":39.92998577808024},"precise":0,"confidence":10,"level":"city"}}"},{address: "Shenzhen",res: "{"status":0,"result":{"location":{"lng":114.0259736573215,"lat":22.546053546205248},"precise":0,"confidence":14,"level":"city"}}"},{address: "Guangzhou City",res: "{"status":0,"result":{"location":{"lng":113.30764967515182,"lat":23.12004910207623},"precise":0,"confidence":12,"level":"city"}}"},{address: "Puning City",res: "{"status":0,"result":{"location":{"lng":116.07816590835329,"lat":23.28895358314155},"precise":0,"confidence":14,"level":"district"}}"}]Okay, there is no problem with batch query. Next, we will use nodejs to read the excel file thrown by the background engineer.
3. Nodejs read and write files
This time we need two more dependencies, one is built-in FS module for nodejs, and one is node-xlsx library for reading and writing excel
Throw the excel file of the city to the root directory and start another script xls2js.js:
var xlsx = require('node-xlsx') , fs = require('fs') ;var file_path = './query_result.xlsx';var file_data = xlsx.parse(file_path);Then call fs.writeFile to write the extracted city to the following code:
file_data.forEach(function(sheet, index) { var sheetname = sheet.name // Table name, sheetdata = sheet.data // Table data, sheethead = sheetdata[0] // The first row is generally a header, but not a certain one, sheetbody = sheetdata.slice(1) // Real data, file_path_towrite = './static/address.json' , file_data_json , cities_name = [] ; // Write the city's data into sheetbody.forEach(function(e, i) { cities_name.push('' + e[1] + ',' + e[2]) }) file_data_json = JSON.stringify({cities_name: cities_name}); fs.writeFile(file_path_towrite, file_data_json, function(err) { if (err) console.log('Write data failed', err); else console.log('Write file successfully'); })})Open the static/address.json file and you will see text in the following format:
{"cities_name":["Beijing, Beijing","Beijing, municipal district","Tianjin, Tianjin"]}4. Comprehensive steps 2 and 3 to realize an interface that reads local city files, batches of queries, and writes new files
OK, with this file, we can read it again and then perform batch query:
app.get('/', function(req, res, next) { var sk = 'yoursk' , addresses = [] , file_path = './static/address.json' , file_path_towrite = './static/geocoder.json' , file_data ; fs.readFile(file_path, function(err, data) { if (err) { console.log('Failed to read the file', err); return; } file_data = JSON.parse(data); addresses = file_data.cities_name; ep.after('getLocation', addresses.length, function(locations) { var file_data = {}; locations.forEach(function(e, i) { file_data[e.address.split(',')[1]] = [e['location']['lng'], e['location']['lat']]; }) fs.writeFile(file_path_towrite, JSON.stringify(file_data), function(err) { if (err) console.log('Write data failed', err); else console.log('Get data and write file successfully'); res.send(file_data); }) }) addresses.forEach(function(e, i) { superagent.get('http://api.map.baidu.com/geocoder/v2/') .query({address: e.split(',').join(' ')}) .query({city: e.split(',')[1]}) .query({output: 'json'}) .query({ak: sk}) .end(function(err, sres) { var location , res_json ; res_json = JSON.parse(sres.text); if (res_json.status == 0) { location = res_json.result && res_json.result.location || ''; } else { location = {"lng":0,"lat":0}; } ep.emit('getLocation', {address: e, location: location}) }) }) }) }) });})5. Implement a web page, which can input the geographical location to conduct batch query of the geographical location.
These are the front-end things, how to write them if you look good
6. Summary
The above is all the content of using NodeJS to implement batch query of latitude and longitude interfaces. I hope it will be helpful to everyone using nodejs.