3

I found npm module google-maps-api and installed it (npm install google-maps-api) but I can't figure out how to import it with systemjs/jspm (jspm cannot find this module). Here's the configuration from my config.js:

"paths": {
"*": "app/dist/*.js",
"github:*": "app/jspm_packages/github/*.js",
"npm:*": "app/jspm_packages/npm/*.js" }

So, when I try do something like this:

import {mapsapi} from 'google-maps-api';

I get the following error in browser console:

GET https://localhost:44308/app/dist/google-maps-api.js 404 (Not Found)

Looking at the filesystem I see that npm installed the module under app/node_modules/google-maps-api so how do I reference it in the import clause from Aurelia module?

3 Answers 3

6

I found a solution and answering my own question here:

I finally figured how to install it with jspm, so you just need to give a hint to jspm to install it from npm like so:

jspm install npm:google-maps-api

After jspm completes installation, import (no {} syntax) works fine:

import mapsapi from 'google-maps-api';

then I inject it in constructor and instantiate geocoder api:

@inject(mapsapi('InsertYourGMAPIKeyHere'))
export class MyClass {         
    constructor(mapsapi) {
        let that = this;
        let maps = mapsapi.then( function(maps) {
            that.maps = maps;
            that.geocoder = new google.maps.Geocoder();
        });
...
}

In order to create map on a div I use EventAggregator to subscribe for router:navigation:complete event and use setTimeout to schedule map creation:

        this.eventAggregator.subscribe('router:navigation:complete', function (e) {
        if (e.instruction.fragment === "/yourRouteHere") { 
            setTimeout(function() {
                that.map = new google.maps.Map(document.getElementById('map-div'),
                {
                    center: new google.maps.LatLng(38.8977, -77.0366),
                    zoom: 15
                });
            }, 200);
        }
    });
5
  • tried activate()? (or attached() if it's a custom element) Jul 3, 2015 at 7:26
  • Assuming the same activation event model as Durandal (plausible but not guaranteed) you want the compositionComplete event. I take a difference approach: for something like maps which generally commandeer the whole window I just put a general purpose container in the shell markup styled to fully occupy the window but with a low z-index. That way the div is available at all stages in view lifecycle, and you get the same origin and coordinate system but your views float over. I find that having each toolwindow managed by a viewmodel produces lovely tidy code.
    – Peter Wone
    Jul 9, 2015 at 0:29
  • does the google-maps-api library support loading libraries? like the places library? That's what I'm trying to figure out... Jul 23, 2015 at 23:02
  • Yes, you can load the places library. I just wrote a small tutorial at blog.thecoderecipe.com/articles/aurelia/places/2015/12/06/… and a sample at github.com/TheCodeRecipe/sample-aurelia-places-autocomplete. I hope that helps! Dec 6, 2015 at 0:52
  • Note that if you are using TypeScript, as for TypeScript 1.7, you need use "import * as mapsapi from 'google-maps-api';" Also, the library supports Google Maps for business and other parameters. See npmjs.com/package/google-maps-api for detail.
    – Morio
    Feb 22, 2016 at 17:50
6

Here's a complete view-model example that uses attached() to link to your view.

import {inject} from 'aurelia-framework';
import mapsapi from 'google-maps-api';

@inject(mapsapi('your map key here'))

export class MapClass {

    constructor(mapsAPI) {

        this.mapLoadingPromise = mapsAPI.then(maps => {
            this.maps = maps;
        });
    }

    attached() {

        this.mapLoadingPromise.then(() => {

            var startCoords = {
                lat:  0,
                long: 0
            };

            new this.maps.Map(document.getElementById('map-div'), {
                center: new this.maps.LatLng(startCoords.lat, startCoords.long),
                zoom:   15
            });
        });
    }
}
2

For everyone using Typescript and getting "Cannot find module 'google-maps-api'" error, you need to add typings to the solution. Something like this works

declare module 'google-maps-api' {

    function mapsapi(apikey: string, libraries?, onComplete?);

    namespace mapsapi {
    }

    export = mapsapi;
}

and then import it like this

import * as mapsapi from 'google-maps-api';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.