地理编码?将坐标列添加到点数据集中

问题描述 投票:0回答:1

如果我有一个包含行业名称和所在城市的数据集,有没有办法可以从谷歌地图自动获取地址或坐标?我尝试询问 ChatGPt,后者告诉我使用 Geoocoding。

我需要在数据集中创建一个包含地址或坐标的列,以便我可以在地图上对它们进行地理定位。我只有城市栏。

示例:Smaltimento Rifiuti Ecoiso |那不勒斯。我需要另外两列,其中包含从 Google 地图获取的坐标,或另一列包含地址(第一列更好)

尝试获取 Google api

geolocation gis google-geocoding-api
1个回答
0
投票

此代码使用 Google Places API 获取不同城市各个行业的地址和

Geolocation
(纬度和经度)。它迭代包含行业名称和城市的数据集,发出 API 请求来检索地点 ID 和详细信息,然后使用地址和位置信息记录结果。目标是使用 Google Places API 收集每个行业-城市对的地理数据

两个 Google API

您需要启用两个Google API 地点 API

地图 JavaScript API

您需要从您的Google帐户项目中复制API Key。

https://console.cloud.google.com/cloud-resource-manager

更多详细信息请参见这里

演示代码

此代码将解决您的问题。

另存为

get-address.js

const axios = require('axios');

const API_KEY = 'Your Google Maps API key';

// your data sat
const data = [
    { industry: 'Smaltimento Rifiuti Ecoiso', city: 'Napoli' },
    { industry: 'Automotive Manufacturing', city: 'Turin' },
    { industry: 'Fashion Design', city: 'Milan' },
    { industry: 'Wine Production', city: 'Florence' },
    { industry: 'Maritime Shipping', city: 'Genoa' },
    { industry: 'Tourism Hospitality', city: 'Venice' },
    { industry: 'Food Processing', city: 'Bologna' },
    { industry: 'Ceramics Production', city: 'Faenza' },
];

async function getPlaceDetails(placeId) {
    try {
        const response = await axios.get('https://maps.googleapis.com/maps/api/place/details/json', {
            params: {
                place_id: placeId,
                key: API_KEY
            }
        });

        if (response.data.result) {
            const { formatted_address, geometry } = response.data.result;
            const { lat, lng } = geometry.location;
            return { address: formatted_address, location: { latitude: lat, longitude: lng } };
        } else {
            throw new Error('No details found for place ID: ' + placeId);
        }
    } catch (error) {
        throw new Error('Error fetching place details: ' + error.message);
    }
}

async function searchPlaceId(cityName, industryName) {
    try {
        const response = await axios.get('https://maps.googleapis.com/maps/api/place/textsearch/json', {
            params: {
                query: industryName + ' in ' + cityName,
                key: API_KEY
            }
        });

        if (response.data.results.length > 0) {
            const placeId = response.data.results[0].place_id;
            return placeId;
        } else {
            throw new Error('No results found for query: ' + industryName + ' in ' + cityName);
        }
    } catch (error) {
        throw new Error('Error fetching place ID: ' + error.message);
    }
}

async function getAddressAndLocationForIndustryAndCity(industryName, cityName) {
    try {
        const placeId = await searchPlaceId(cityName, industryName);
        const { address, location } = await getPlaceDetails(placeId);
        return { industry: industryName, city: cityName, address, location };
    } catch (error) {
        throw new Error('Error getting address and location: ' + error.message);
    }
}

async function fetchData(data) {
    const results = [];
    for (const { industry, city } of data) {
        try {
            const result = await getAddressAndLocationForIndustryAndCity(industry, city);
            results.push(result);
        } catch (error) {
            console.error(`Error processing ${industry}, ${city}: ${error.message}`);
        }
    }
    return results;
}

fetchData(data)
    .then(results => {
        console.log('Address and location for each industry:');
        console.log(results);
    })
    .catch(error => {
        console.error('Error:', error.message);
    });

输入数据

    { industry: 'Smaltimento Rifiuti Ecoiso', city: 'Napoli' },
    { industry: 'Automotive Manufacturing', city: 'Turin' },
    { industry: 'Fashion Design', city: 'Milan' },
    { industry: 'Wine Production', city: 'Florence' },
    { industry: 'Maritime Shipping', city: 'Genoa' },
    { industry: 'Tourism Hospitality', city: 'Venice' },
    { industry: 'Food Processing', city: 'Bologna' },
    { industry: 'Ceramics Production', city: 'Faenza' },

输出

[
  {
    industry: 'Smaltimento Rifiuti Ecoiso',
    city: 'Napoli',
    address: "Cupa Vicinale Sant'Aniello, 96, 80146 Napoli NA, Italy",
    location: { latitude: 40.82747369999999, longitude: 14.3224339 }
  },
  {
    industry: 'Automotive Manufacturing',
    city: 'Turin',
    address: "S.da Comunale da Bertolla all'Abbadia di Stura, 132, 10156 Torino TO, Italy",
    location: { latitude: 45.1095563, longitude: 7.7345843 }
  },
  {
    industry: 'Fashion Design',
    city: 'Milan',
    address: 'Via Giuseppe Broggi, 7, 20129 Milano MI, Italy',
    location: { latitude: 45.4781573, longitude: 9.2103889 }
  },
  {
    industry: 'Wine Production',
    city: 'Florence',
    address: 'Via dei Pepi, 22, 50122 Firenze FI, Italy',
    location: { latitude: 43.77029899999999, longitude: 11.263162 }
  },
  {
    industry: 'Maritime Shipping',
    city: 'Genoa',
    address: 'Via Galeazzo Alessi, 1, 16128 Genova GE, Italy',
    location: { latitude: 44.4029039, longitude: 8.9384841 }
  },
  {
    industry: 'Tourism Hospitality',
    city: 'Venice',
    address: "Via degli Arditi, 35, 30013 Ca' Ballarin VE, Italy",
    location: { latitude: 45.4737973, longitude: 12.5279771 }
  },
  {
    industry: 'Food Processing',
    city: 'Bologna',
    address: 'Via Francesco Zanardi, 54, 40131 Bologna BO, Italy',
    location: { latitude: 44.5136966, longitude: 11.3258413 }
  },
  {
    industry: 'Ceramics Production',
    city: 'Faenza',
    address: 'Piazza 2 Giugno, 7/palazzo Muky, 48018 Faenza RA, Italy',
    location: { latitude: 44.2887423, longitude: 11.8772864 }
  }
]

安装node.js依赖

npm install axios

运行它

node get-address.js

结果

© www.soinside.com 2019 - 2024. All rights reserved.