0

I have added google map in my angularjs application but I am unable to show marker on map. I have gone through a number of post on this topic (like: this and this) but it could not help. Below is my code snippet where lat and long are generated from random number:

HTML:

<ion-view view-title="Map">
    <ion-content data-tap-disabled="true">
        <div id="map">
            <ui-gmap-google-map center="map.center" zoom="map.zoom" draggable="true" options="options">
                <ui-gmap-markers models="markers" coords="'coords'" idKey="'idKey'"></ui-gmap-markers>
            </ui-gmap-google-map>
        </div>        
    </ion-content>     
</ion-view>

Controller:

uiGmapGoogleMapApi.then(function(maps) {
    lat = (Math.random()*(40-20)+20);
    long = (Math.random()*(140-10)+10);
    $scope.map = { center: { latitude: lat, longitude: long }, zoom: 8 };        
});



uiGmapIsReady.promise(1).then(function(instances) {
    instances.forEach(function(inst) {
    var map = inst.map;
    var uuid = map.uiGmap_id;
    var myLatlng = new google.maps.LatLng(lat,long); 
    var mapOptions = { 
        zoom: 4, 
        center: myLatlng 
    } 
    var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);  
    var marker = new google.maps.Marker({ 
        position: myLatlng, 
        map: map, 
        title: 'test!' 
    });

Can anyone please help to know what to change to make the marker seen on the google map. Below is the map shown which currently does not have marker:

Google map

Community
  • 1
  • 1
arin1405
  • 677
  • 1
  • 7
  • 18

2 Answers2

0

I am not able to see the initialize function in your controller code. Please make sure that you define the initialize function in the global context.

angular.module('main', ['ionic'])
    .controller('LocationCtrl', function ($scope, $ionicLoading, $compile, $window) {
    function initialize() {
        google.maps.event.addDomListener(window, 'load');

        var myLatlng = new google.maps.LatLng(43.07493, -89.381388);

        var mapOptions = {
            center: myLatlng,
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"),
        mapOptions);

        //Marker + infowindow + angularjs compiled ng-click
        var contentString = "<div><a ng-click='clickTest()'>Click me!</a></div>";
        var compiled = $compile(contentString)($scope);

        var infowindow = new google.maps.InfoWindow({
            content: compiled[0]
        });

        var marker = new google.maps.Marker({
            //your random marker generation code
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });

        $scope.map = map;
    }

    $window.initialize = initialize; // callback in global context

    function loadScript(src) {
        var script = document.createElement("script");
        script.type = "text/javascript";
        document.getElementsByTagName("head")[0].appendChild(script);
        script.src = src;
    }

    loadScript('http://www.google.com.mt/jsapi');
    loadScript('http://maps.googleapis.com/maps/api/js?key=&v=3&sensor=true&callback=initialize');



    $scope.centerOnMe = function () {
        if (!$scope.map) {
            return;
        }

        $scope.loading = $ionicLoading.show({
            content: 'Getting location',
            showBackdrop: false
        });

        navigator.geolocation.getCurrentPosition(function (pos) {
            $scope.map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
            $scope.loading.hide();
        }, function (error) {
            alert('Unable to get location: ' + error.message);
        });
    };

    $scope.clickTest = function () {
        alert('Example of infowindow with ng-click')
    };
});
AniV
  • 3,997
  • 1
  • 12
  • 17
0

I have removed mapOptions and map as no need to initialize map once again. And marker is coming properly now.

uiGmapIsReady.promise(1).then(function(instances) {

    instances.forEach(function(inst) {
    var map = inst.map;
    var uuid = map.uiGmap_id; 
    var myLatlng = new google.maps.LatLng(lat,long); 
    var marker = new google.maps.Marker({ 
        position: myLatlng, 
        map: map, 
        title: '' 
    });

});

Map with Marker

arin1405
  • 677
  • 1
  • 7
  • 18