如何使用来自4sq节点模块lat long的信息到地理编码器模块中

时间:2018-01-19 20:46:54

标签: javascript node.js google-maps express foursquare

如何使用从foursq节点模块(lat long)收到的信息,并使用promises将其用于地理编码器节点模块。我试图用lat long进行反向搜索。我还想从4sq的节点模块获取信息并创建一个json文件。

const apiKey = '***';
const NodeGeocoder = require('node-geocoder');
const options = {
provider: 'google',
httpAdapter: 'https',
apiKey: apiKey
};
const geocoder = NodeGeocoder(options)

require('request');
const request = require('request-promise-native');
const qs = require('querystring');
const baseFourSqURL = 'https://api.foursquare.com/v2/venues';

const foursquareParams = {
client_id: '***',
client_secret: '***',
query: 'wafflehouse',
v: '20170801',
limit: 10
};

exports.waffleHouseLookup = function (lat, long) {
const params = qs.stringify(Object.assign({
    ll: `${lat},${long}`
}, foursquareParams));

const url = `${baseFourSqURL}/search?${params}`;

return new Promise((resolve, reject) => {
    request.get(url)
        .then(body => {
            try {
                return JSON.parse(body).response.venues.map(venue => {
                    result = {
                        name: venue.name,
                        address: {
                            street: venue.location.address,
                            postalcode: venue.location.postalCode,
                            city: venue.location.city,
                            state: venue.location.state,
                            country: venue.location.country,
                            country_code: venue.location.cc,
                            formatted: venue.location.formattedAddress,
                            lat: venue.location.lat,
                            lng: venue.location.lng,
                        },
                        phone: venue.contact.formattedPhone,
                        distance: ( venue.location.distance / 1609.344 ).toFixed(2),
                        meta: {
                            //...venue
                        }
                    };
                    return result
                });
            } catch (err) {
                return reject({
                    url,
                    error: err,
                });
            }
        })
        .then(wf)
        .then(resolve)
        .catch(err => {
            return reject({
                url,
                error: err,
            })
        });
});
};

const wf = function (lat, long){
    geocoder.reverse({lat:lat, lon:long})
    .then((res)=>{
        formattedAddress = res[0].formattedAddress
        streetNumber = res[0].streetNumber
        streetName = res[0].streetName
        la = res[0].latitude.toFixed(6)
        lng = res[0].longitude.toFixed(6)
        cityName = res[0].city
        postalCode = res[0].zipcode
        country = res[0].country
        state = res[0].administrativeLevels.level1short
        countryCode = res[0].countryCode
        street = `${streetNumber} ${streetName}`
        addr = {
            address: street,
            postalcode: postalCode,
            city: cityName,
            state: state,
            country: country,
            country_code: countryCode,
            formatted_address: formattedAddress,
            lat: la,
            lng: lng
        }
        console.log(addr)
        return Promise.resolve(addr)
    })
    .catch((err)=>{
        console.log(err)
    })
 }

我也在使用expressjs来接受长期4sq api params。

0 个答案:

没有答案