How can I get city name from a latitude and longitude point?

后端 未结 11 1226
感情败类
感情败类 2020-11-28 03:07

Is there a way to get a city name from a latitude and longitude point using the google maps api for javascript?

If so could I please see an example?

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 03:37

    In node.js we can use node-geocoder npm module to get address from lat, lng.,

    geo.js

    var NodeGeocoder = require('node-geocoder');
    
    var options = {
      provider: 'google',
      httpAdapter: 'https', // Default
      apiKey: ' ', // for Mapquest, OpenCage, Google Premier
      formatter: 'json' // 'gpx', 'string', ...
    };
    
    var geocoder = NodeGeocoder(options);
    
    geocoder.reverse({lat:28.5967439, lon:77.3285038}, function(err, res) {
      console.log(res);
    });
    

    output:

    node geo.js

    [ { formattedAddress: 'C-85B, C Block, Sector 8, Noida, Uttar Pradesh 201301, India',
        latitude: 28.5967439,
        longitude: 77.3285038,
        extra: 
         { googlePlaceId: 'ChIJkTdx9vzkDDkRx6LVvtz1Rhk',
           confidence: 1,
           premise: 'C-85B',
           subpremise: null,
           neighborhood: 'C Block',
           establishment: null },
        administrativeLevels: 
         { level2long: 'Gautam Buddh Nagar',
           level2short: 'Gautam Buddh Nagar',
           level1long: 'Uttar Pradesh',
           level1short: 'UP' },
        city: 'Noida',
        country: 'India',
        countryCode: 'IN',
        zipcode: '201301',
        provider: 'google' } ]
    

提交回复
热议问题