Nico Hernandez Torres Nico Hernandez Torres - 2 months ago 56
AngularJS Question

Map inside ionic Popup

I'm trying to insert a map in a popup alert ionic. I have not done and not know why.

var contentHtml = '<map style="width:100%;height:100%;" center="' + club.club_latitud + "," + club.club_longitud + '" zoom="17" data-tap-disabled="true"><marker position="' + club.club_latitud + "," + club.club_longitud + '"/></map>';

$ionicPopup.show({
title: 'InformaciĆ³n Club',
subTitle: club.club_nombre,
content: contentHtml,
buttons: [{
text: 'Aceptar',
type: 'button-positive',
onTap: function(e) {

}
}]
})


Within the body of the popup if all the data, latitude, longitude and also the market but the map is not displayed are loaded.

Answer

I have done something similar recently. Get this library via bower angular-google-maps

Add <script src="lib/angular-google-maps/dist/angular-google-maps.min.js"></script> to your index.html

Then add this function to a service or factory

angular.module('app.services')
    .factory('LocationService', LocationService);

LocationService.$inject = ['$q', '$ionicModal', '$timeout', '$ionicPopup', '$ionicLoading', '$rootScope', '$cordovaGeolocation', 'uiGmapGoogleMapApi'];

function FileService($q, $ionicModal, $timeout, $ionicPopup, $ionicLoading, $rootScope, $cordovaGeolocation, uiGmapGoogleMapApi) {
    return {
        selectLocation: selectLocation
    };

function selectLocation(mapCenter) {
        var def = $q.defer();
        var locationScope = $rootScope.$new();
        locationScope.location = {};
        locationScope.geocodeAddress = geocodeAddress;
        locationScope.chooseLocation = chooseLocation;
        locationScope.cancelModal = cancelModal;



        $ionicModal.fromTemplateUrl('some/folder/locationpicker.html', {
            scope: locationScope,
            animation: 'slide-in-up'
        }).then(function (modal) {

            locationScope.modal = modal;
            locationScope.modal.show().then(function () {
                var mapOptions = {
                    zoom: 15,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var geocoder = new google.maps.Geocoder;
                locationScope.map = new google.maps.Map(document.getElementById("map"), mapOptions);

                google.maps.event.addListener(locationScope.map, "dragend", function () {
                    locationScope.geocodeLoading = true;
                    var mapCenter = locationScope.map.getCenter();
                    var latlng = { lat: mapCenter.lat(), lng: mapCenter.lng() };
                    var addressReqUrl = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + latlng.lat + ',' + latlng.lng + '&';
                    locationScope.geocodeRequestCanceller = $q.defer();
                    locationScope.geocodeTimeout = $timeout(function () {
                        $http.get(addressReqUrl, { timeout: locationScope.geocodeRequestCanceller }).then(function (response) {
                            locationScope.location = {
                                latitude: mapCenter.lat(),
                                longitude: mapCenter.lng(),
                                address: response.data.results[0].formatted_address
                            };
                            delete locationScope.geocodeRequestCanceller;
                            locationScope.geocodeLoading = false;
                        });
                    }, 500);

                });

                google.maps.event.addListener(locationScope.map, "dragstart", function () {
                    if (locationScope.geocodeTimeout) {
                        $timeout.cancel(locationScope.geocodeTimeout);
                    }
                    if (locationScope.geocodeRequestCanceller) {
                        locationScope.geocodeRequestCanceller.resolve();
                        delete locationScope.geocodeRequestCanceller;
                    }
                });
                if (mapCenter) {
                    locationScope.location.address = mapCenter.address;
                    setLocation(mapCenter.latitude, mapCenter.longitude);
                }
            });
        });

        function setLocation(lat, lng) {
            var latLng = new google.maps.LatLng(lat, lng);
            locationScope.map.setCenter(latLng);
        }

        function geocodeAddress(address) {
            var geocoder = new google.maps.Geocoder();

            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    $timeout(function () {
                        var location = results[0].geometry.location;
                        setLocation(location.lat(), location.lng());
                        angular.extend(locationScope.location, {
                            latitude: location.lat(),
                            longitude: location.lng(),
                            address: results[0].formatted_address
                        });
                    }, 1);
                } else {
                    console.log('Geocode was not successful for the following reason: ' + status);
                }
            });
        }

        function cleanUp() {
            locationScope.modal.hide();
            locationScope.modal.remove();
        }

        function cancelModal() {
            cleanUp();
            def.resolve();
        }

        function chooseLocation() {
            if (!locationScope.location) {
                $ionicPopup.alert({
                    title: 'Validation Error!',
                    template: 'You have to select a location'
                });
                return;
            }
            var location = angular.extend({}, locationScope.location);
            var mAddr = location.latitude + "," + location.longitude;
            location.mapUrl = 'https://maps.googleapis.com/maps/api/staticmap?zoom=15&scale=1&size=600x400&markers=' + mAddr + '&center=' + mAddr
            cleanUp();
            def.resolve(location);
        }
        return def.promise;
    }
 }

Locationpicker.html

<ion-modal-view>

    <ion-content>
        <div class="list">

            <div class="item item-input-inset">

                <label class="item-input item-input-wrapper">
                   <i class="icon ion-search placeholder-icon"></i>
   <input ng-model="postcode" type="search" ng-enter="geocodeAddress(postcode)" placeholder="Postcode/Address">
                </label>
                <button ng-click="geocodeAddress(postcode)" ng-class="{'ion-search' : !!map, 'button-clear' : !map }" class="button icon button-small button-balanced">
                    <ion-spinner ng-if="!map" class="spinner-balanced" icon="spiral"></ion-spinner>
                </button>
            </div>
            <div class="item item-image">
                <div id="map" data-tap-disabled="true"></div>
            </div>
            <div class="item item-text-wrap pi-small-text">
                {{location.address}}
            </div>
        </div>

    </ion-content>
    <div class="bar bar-footer">
        <button class="button button-clear button-assertive" ng-click="cancelModal()">Cancel</button>
        <div class="title"></div>
        <button class="button button-clear button-positive" ng-click="chooseLocation()">
            <span ng-if="!geocodeLoading">Choose</span>
            <ion-spinner ng-if="geocodeLoading" class="spinner-balanced" icon="spiral"></ion-spinner>
        </button>
    </div>
</ion-modal-view>

Usage

LocationService.selectLocation(latlng)
                        .then(function (location) {
                            if (location) {
                                vm.newLocation = { lat: location.latitude, lng: location.longitude, address: location.address, mapUrl: location.mapUrl };
                                }
                        });

Probably has a lot more than you need, I'll leave trimming off the excess to you.