#Maps with Nest

9 messages · Page 1 of 1 (latest)

vernal storm
#

¡Hi! Has anyone ever implemented a connection between NestJS with Mapbox or Google Maps? What I want is to collect data from the current location and save addresses.

#

If anyone knows of a resource or repository where I can guide me and use Nest, I appreciate it a lot.

fervent jetty
#

If you want to get the country and the city you can use something like this if you have the lon and the lat

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as NodeGeocoder from 'node-geocoder';

@Injectable()
export class LocationsService {
  constructor(private readonly configService: ConfigService) {}

  async locateCityAndCountry(GEOLocation: { lat: number; lon: number }) {
    const options: NodeGeocoder.Options = {
      provider: 'mapquest',
      httpAdapter: 'https',
      apiKey: this.configService.get<string>('MAP_QUEST_API_KEY'),
      formatter: 'json',
    };

    const geocoder = NodeGeocoder(options);

    const GEOData = await geocoder.reverse(GEOLocation);

    return {
      city: GEOData[0]?.city,
      country: GEOData[0]?.country || GEOData[0]?.countryCode,
    };
  }
}
#

You can replace the apiKey with the one you get from Mapbox or google maps

vernal storm
#

@fervent jetty Excellent, thank you very much!

fervent jetty
#

Np

vernal storm
#

@fervent jetty Hi, I've a question, the implementation you gave me was very useful but I would like to know if you know of a scheme to do something like this: What I would need is somehow, receive some coordinates and save the info of the city and so on in database (so far there is nothing new, with the implementation that you gave me you can), but to save costs of calls to the mapbox api or google maps api, I would like to somehow save the coordinates of a complete city for when coordinates arrive, validate if they are part of any city in database, in case yes, I would not consult an external API but I associate them to that respective city.

Do you know a way to do this? Thank you in advance for your support.

fervent jetty
#

I don't have something ready but this is doesn't require any new knowledge you have to implement the logic based on how you described but I wouldn't recommend something like what you described.

A better solution would be to host an ArcGIS Server or a GEO Server, and use it a base for holding GEO info, this will save you space and time on your backend DB and so on.

vernal storm
#

@fervent jetty I mean, at the level of a large-scale application that is already in production, it is preferable to avoid saving that kind of costs and better to interact directly with a third party such as Mapbox or Google Maps API?