Google Maps
Add Google Maps and Geolocation to your Ionic App. Learn how we integrated Maps to Ion2FullApp Ionic Template for very different use cases.
Installation
To use Google Maps Javascript SDK we have to add this line in index.html
You need to get a key (it’s free) in https://developers.google.com/maps/documentation/javascript/get-api-key
Then we have to install the following typing:
Error: Cannot find namespace Google
If you did the above configurations and you are still getting the message: “Cannot find namespace Google” try doing the following:
Install the TypeScript Definition Manager CLI
Then use your CLI to navigate into your Ionic 3 project's folder
Install the google map definition files using the typings CLI
typings install dt~google.maps --global
For Windows Users
Some Windows users reported that their fix was to add the following in tsconfig.json:
"typeRoots": ["node_modules/@types"],
within the "compilerOptions"
There are two ways to implement Google Maps:
Web based - Javascript
Native
But what’s the difference between these two options and which one is better?
There are a few points we should consider:
Javascript SDK
Native SDKs
Conclusions
Scored on those points above, it’d be a tie between the JavaScript SDK and the Native SDK, so the answer to the question of “which one is better?” would be the ever popular: it depends. You’ll have to consider the requirements of your application and make a decision based on that.
Implementation
In this app we decided to use the Javascript SDK because we have an open mind about progressive web apps feature.
In order to interact with this Google Maps library properly from angular we created the following elements:
Directive
We created a directive <google-map>
to display the map and define a bridge to the google.maps.Map object. You can find it in src/components/google-map/google-map.ts
Service
We created a service to interact with all google maps API’s we used in the example. You can find it in src/pages/maps/maps.service.ts
google.maps.places.AutocompleteService
Provides places suggestions for the search and autocomplete feature
google.maps.Geocoder
Handles the geocoding. This means getting the location (lat, lng) from a place and vice-versa.
google.maps.places.PlacesService
To search and display nearby places
google.maps.DirectionsService
To get and display directions from point A to point B
google.maps.DistanceMatrixService
To get distances between two or more points
Models
In order to handle all these functionalities clearly in one page we created some angular models to helps us get things tidy and understandable. You can find them in src/pages/maps/maps.model.ts
NgZones
One caveat worth mentioning here is that there are known issues with external libraries interacting with the angular zone. In this particular case the issue is because the google.maps API’s are not patched by Angular's zone. We fixed the issue using angular NgZones in the service when dealing with the Observers used to interact with google maps API’s endpoints.
You can find more information about these issues here:
These are great posts that will help you understand NgZones:
Icons
We also added some fancy stuff like the custom icons. According to google maps API there are two ways of customizing icons.
Using images
Using symbols (svg)
We followed the second option as we think is the best approach. If you want to change the icons symbol you need to find the path of the svg you want and set that value on the icon object. You can find more information here:
Distance between two points
Static Maps
GEOLOCATION
To install run:
Last updated