cordova-plugin-googlemaps icon indicating copy to clipboard operation
cordova-plugin-googlemaps copied to clipboard

v1.3.9 : addMarker & animateCamera are not working (with & without Ionic 2 wrapper)

Open Geoffrey-D opened this issue 10 years ago • 9 comments

Hello,

The method addMarker() is not working on Ionic 2 RC-0 with v1.3.9.

The map is correctly displayed, we can interact with it (with methods like map.setCenter(myPosition) ) but the addMarker is not working : the Promise is not resolved and it does not throw any exception.

google-map.html

<ion-header>
  <ion-navbar>
    <ion-title>Google Map</ion-title>
  </ion-navbar>
</ion-header>

<ion-content class="home">
    <div id="map"></div>
</ion-content>

google-map.scss (to fix display issue) :


ion-app._gmaps_cdv_ .nav-decor{
  background-color: transparent !important;
}

.home {
    #map {
        height: 100%;
    }
}


google-map.ts :

import { Component } from '@angular/core';
import {Platform} from 'ionic-angular';
import { GoogleMap, GoogleMapsEvent, GoogleMapsLatLng , CameraPosition, GoogleMapsMarker, GoogleMapsMarkerOptions } from 'ionic-native';
import { Geolocation } from 'ionic-native';

@Component({
  templateUrl: 'google-map.html'
})
export class GoogleMapPage {

  constructor(public platform: Platform){
     platform.ready().then(() => {
            this.loadMap();
        });
    }

    loadMap () {
        let map = new GoogleMap('map');
        GoogleMap.isAvailable().then(() => {
            map.one(GoogleMapsEvent.MAP_READY).then(() => {
                Geolocation.getCurrentPosition().then((resp) => {
                    let myPosition: GoogleMapsLatLng = new GoogleMapsLatLng(resp.coords.latitude, resp.coords.longitude);
                    map.setCenter(myPosition); // working
                    map.setZoom(15); // working
                    map.setMyLocationEnabled(true); // working
                    map.setDebuggable(true);
                    map.getCameraPosition().then((resp) => {
                        console.log(resp); // working
                    });

                    let markerOptions: GoogleMapsMarkerOptions = {
                        position: myPosition,
                        title: 'My Position'
                    };
                        map.addMarker(markerOptions)
                            .then((marker: GoogleMapsMarker) => {
                                console.log('marker ADDED'); // never fired
                                marker.showInfoWindow();
                            }).catch((e) => {console.log(e);}); // never fired
                });
            });

        });

    }

}

Am I missing something here?

Thanks for your help !

Geoffrey

Geoffrey-D avatar Oct 14 '16 10:10 Geoffrey-D

Why don't you try master branch?

guillenotfound avatar Oct 14 '16 10:10 guillenotfound

hello @ZiFFeL1992 , I just switched to the master branch with :

ionic plugin remove cordova-plugin-googlemaps --save ionic plugin add https://github.com/mapsplugin/cordova-plugin-googlemaps.git --variable [...] --save

But it doesn't fix the issue : no marker are created and nothing in console :/

Geoffrey-D avatar Oct 14 '16 10:10 Geoffrey-D

Try to work without the wrapper (without ionic-native), use the plugin as described in the wiki.

Put declare var plugin: any; after your imports to avoid compilation to fail.

guillenotfound avatar Oct 14 '16 10:10 guillenotfound

I have reinstall the app with the command :

cordova plugin add cordova-plugin-googlemaps --variables [...] --save

I have added declare var plugin: any; after the imports,

and it still doesn't work with that loadMap function (from the wiki) :


    loadMap () {
        let mapProp2 = {
            'backgroundColor': 'white',
            'mapType': plugin.google.maps.MapTypeId.ROADMAP,
            'controls': {
                'compass': true,
                'myLocationButton': true,
                'indoorPicker': true,
                'zoom': true
            },
            'gestures': {
                'scroll': true,
                'tilt': true,
                'rotate': true
            }
        };

        let map = plugin.google.maps.Map.getMap(document.getElementById('map'), mapProp2);

        map.addMarker({
            position: {lat: 50.854954, lng: 4.3053508},
            title: "Welecome to \n" +
            "Cordova GoogleMaps plugin for iOS and Android",
            snippet: "This plugin is awesome!",
            animation: plugin.google.maps.Animation.BOUNCE
        }, function (marker) {
            console.log('marker added?'); // NEVER FIRED

            // Show the info window
            marker.showInfoWindow();

            // Catch the click event
            marker.on(plugin.google.maps.event.INFO_CLICK, function () {

                // To do something...
                alert("Hello world!");

            });
        });


        map.animateCamera({
            target: {lat: 37.422359, lng: -122.084344},
            zoom: 17,
            tilt: 60,
            bearing: 140,
            duration: 5000
        }, function () {
            console.log('camera animated ?');  // NEVER FIRED

        });
    }

The map is displayed, but no marker are added, and the animateCamera is not working either.

Geoffrey-D avatar Oct 14 '16 12:10 Geoffrey-D

If you share a repo with me I'll review it when I have time :D

guillenotfound avatar Oct 14 '16 13:10 guillenotfound

@ZiFFeL1992 thank you for your proposal, but I finally figured out what was the issue : when you call addMarker or animateCamera straight after initiating the map, it won't works (and will throw no error) :

NOT WORKING :

    let map = plugin.google.maps.Map.getMap(document.getElementById('map'), mapProp2);

    map.animateCamera({
        target: {lat: 50, lng: 4},
        zoom: 14,
        tilt: 60,
        bearing: 140,
        duration: 2000
    }, function () { });

WORKING :

    let map = plugin.google.maps.Map.getMap(document.getElementById('map'), mapProp2);

       // delay animateCamera with an async call make it work
    Geolocation.getCurrentPosition().then((resp) => {

        map.animateCamera({
            target: {lat: 50, lng: 4},
            zoom: 14,
            tilt: 60,
            bearing: 140,
            duration: 2000
        }, function () { });
    });

To my opinion, this is a bug that should be fixed.

Geoffrey-D avatar Oct 14 '16 15:10 Geoffrey-D

I think you just forget to wait the MAP_READY event.

wf9a5m75 avatar Oct 14 '16 15:10 wf9a5m75

@wf9a5m75 yes, you are right ! I will try to make it work with the ionic 2 wrapper now, so I can close the issue :)

Geoffrey-D avatar Oct 14 '16 15:10 Geoffrey-D

well, the problem with the Ionic 2 wrapper is different, as the map is already loaded :

   let map = new GoogleMap('map');
        GoogleMap.isAvailable().then(() => {
            map.one(GoogleMapsEvent.MAP_READY).then(() => {

I will stick to the "raw" plugin for the moment, as the Ionic 2 wrapper seems broken.

Geoffrey-D avatar Oct 14 '16 16:10 Geoffrey-D