DEV Community

GreggHume
GreggHume

Posted on

3 3

Google map loader api, how to include libraries like places

You want to include "drawing" | "geometry" | "localContext" | "places" | "visualization"?

Here you go:

import { Loader } from "@googlemaps/js-api-loader"

const loader = new Loader({
  apiKey: API_KEY,
  mapId: MAP_ID,
  version: "weekly",
  libraries: ["places"]
});
Enter fullscreen mode Exit fullscreen mode

If you were trying to access places on the google map instance it would have failed because it was not included. Now with adding the library it will work, here is some example code:

var service = new google.maps.places.PlacesService(map);

const request = {
  query: value,
  fields: ['name', 'geometry']
}

service.findPlaceFromQuery(request, function(results, status) {
  if (status === google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      const place = results[i];
      if (!place.geometry || !place.geometry.location) return;

      new google.maps.Marker({
        map: map,
        position: place.geometry.location,
      });
    }
    map.setZoom(13);
    map.setCenter(results[0].geometry.location);
  }
});
Enter fullscreen mode Exit fullscreen mode

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

Top comments (0)

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay