app.get('/one', function(req, res, next) {
var sk = 'yoursk' // 创建应用的sk
, address = '北京市'
;
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);
})
})
{
status: 0,
result: {
location: {
lng: 116.39564503787867,
lat: 39.92998577808024
},
precise: 0,
confidence: 10,
level: "城市"
}
app.get('/many', function(req, res, next) {
var sk = 'yoursk'
, addresses = ['北京市', '深圳市', '广州市', '普宁市']
;
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})
})
})
})
[
{
address: "北京市",
res: "{"status":0,"result":{"location":{"lng":116.39564503787867,"lat":39.92998577808024},"precise":0,"confidence":10,"level":"城市"}}"
},
{
address: "深圳市",
res: "{"status":0,"result":{"location":{"lng":114.0259736573215,"lat":22.546053546205248},"precise":0,"confidence":14,"level":"城市"}}"
},
{
address: "广州市",
res: "{"status":0,"result":{"location":{"lng":113.30764967515182,"lat":23.12004910207623},"precise":0,"confidence":12,"level":"城市"}}"
},
{
address: "普宁市",
res: "{"status":0,"result":{"location":{"lng":116.07816590835329,"lat":23.28895358314155},"precise":0,"confidence":14,"level":"区县"}}"
}
]
var xlsx = require('node-xlsx')
, fs = require('fs')
;
var file_path = './query_result.xlsx';
var file_data = xlsx.parse(file_path);
file_data.forEach(function(sheet, index) {
var sheetname = sheet.name // 表格名称
, sheetdata = sheet.data // 表格的数据
, sheethead = sheetdata[0] // 第一行一般为表头,但不是一定的
, sheetbody = sheetdata.slice(1) // 真正的数据
, file_path_towrite = './static/address.json'
, file_data_json
, cities_name = []
;
// 将城市的数据写进去
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('写入数据失败', err);
else
console.log('写入文件成功');
})
})
{"cities_name":["北京市,北京市","北京市,市辖区","天津市,天津市"]}
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('读取文件失败', 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('写入数据失败', err);
else
console.log('获取数据并写入文件成功');
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})
})
})
});
})
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有