Ionic 4/Angular - Geolocation Plugin, Google Maps and Places API [Part 2]

On the previous part we have added a Google Map to our Ionic 4 application and used the Cordova Geolocation plugin and its Ionic native wrapper to get the current user position.

Check out the first part:

Ionic 4 - Geolocation Plugin, Google Maps and Places API

On this part we are going to continue building our nearby restaurants mobile app so let's get started.

First of all, you need to change the script tag for including the Google Maps JavaScript SDK in the src/index.html file to enable the Places API:

<script src=""></script>

Next open the src/pages/home/home.scss file and add these styles:

page-home {
    .scroll {
        height: 100%

    #map {
        width: 100%;
        height: 40%;

The Google map will take only 40% of height.

Next, open the src/pages/home/home.html file and change it accordingly:

    Nearby Me Restaurants

<ion-content padding>

<div #map id="map"></div> 

<div style="width : 100% ;height: 60%">
    <ion-item *ngFor="let place of places">


Now, in the src/pages/home/home.ts file, add a member variables of type Array to hold the nearby places:

declare var google;

selector: 'page-home',
templateUrl: 'home.html'
export class HomePage {
options : GeolocationOptions;
currentPos : Geoposition;
places : Array<any> ; 

Next, add a getRestaurants() method to get the list of the nearby restaurants:

    var service = new google.maps.places.PlacesService(;
    let request = {
        location : latLng,
        radius : 8047 ,
        types: ["restaurant"]
    return new Promise((resolve,reject)=>{
            if(status === google.maps.places.PlacesServiceStatus.OK)



This method returns a Promise which resolves with the list of restaurants or if there is an error it will be rejected with the status code.

We have wrapped the service.nearbySearch() method, which uses callbacks, in a Promise to play nice with the Angular change detection.

Also add a createMarker() method:

    let marker = new google.maps.Marker({
    animation: google.maps.Animation.DROP,
    position: place.geometry.location

This method creates a marker from a place. It's called for every place returned by the nearbySearch() method.

Finally update the addMap() method to invoke the previous methods:


    let latLng = new google.maps.LatLng(lat, long);

    let mapOptions = {
    center: latLng,
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    } = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

    this.getRestaurants(latLng).then((results : Array<any>)=>{
        this.places = results;
        for(let i = 0 ;i < results.length ; i++)



The places variable holds the list of the nearby restaurants which are then displayed using <ion-list> on the view.


That is it! You have now created a nearby app with Ionic 4/Angular which gets the current user location using the Cordova Geolocation plugin and Ionic Native then uses the Google Places API to search for the nearby restaurants and display them in a Google Map.

Note: We also publish our tutorials on Medium and If you prefer reading in these platforms, you can follow us there to get our newest articles.

You can reach the author via Twitter:

About the author

Ahmed Bouchefra
is a web developer with 5+ years of experience and technical author with an engineering degree on software development. You can hire him with a click on the link above or contact him via his LinkedIn account. He authored technical content for the industry-leading websites such as SitePoint, Smashing, DigitalOcean, RealPython, freeCodeCamp, JScrambler, Pusher, and Auth0. He also co-authored various books about modern web development that you can find from Amazon or Leanpub

Get our Learn Angular 8 in 15 Easy Steps ebook in pdf, epub and mobi formats, plus a new Angular 8 tutorial every 3 days.

comments powered by Disqus Protection Status