google maps with ionic

前端 未结 6 1879
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 12:23

Im trying to implement a map using google maps with Ionic. I followed the coding in this Link But all i get is a blank screen dont know where i went wrong. Please help

相关标签:
6条回答
  • 2020-12-31 12:47

    If you are using the latest version of Ionic, chances are you will experience a blank screen when you deploy for android but you will see the map when u run the solution in a web browser. To resolve you need to add a whitelist plugin, run: ionic plugin add cordova-plugin-whitelist.

    0 讨论(0)
  • 2020-12-31 12:48

    or just replace this

    google.maps.event.addDomListener(window, 'load', initialize);
    

    for this

    ionic.Platform.ready(initialize);
    

    reference http://codepen.io/mircobabini/developer/ionic-google-maps-nightly

    0 讨论(0)
  • 2020-12-31 12:49

    I changed a few things to get this to work:

    You don't need an api key for google maps anymore, this is enough for the script src (see Whats the Google Maps API Key):

    <script src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
    

    Replace "function initialize()" with "$scope.init()" and comment out the line where it sais:

    //google.maps.event.addDomListener(window, 'load', initialize);
    

    Also add ng-init="init()" to your html like this:

    <ion-header-bar class="bar-dark" ng-init="init()">
        <h1 class="title">Map</h1>
    </ion-header-bar>
    

    I don't know why it's not working with the domListener, but the data need to be initialized before displaying them (see Google MAP API Uncaught TypeError: Cannot read property 'offsetWidth' of null). With ng-init you can achieve this.

    Final controller should look like this:

    .controller('MapCtrl', function($scope, $ionicLoading, $compile) {
    
        $scope.init = function() {
            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({
              position: myLatlng,
              map: map,
              title: 'Uluru (Ayers Rock)'
            });
    
            google.maps.event.addListener(marker, 'click', function() {
              infowindow.open(map,marker);
            });
    
            $scope.map = map;
        };
    
        // google.maps.event.addDomListener(window, 'load', initialize);
    
        $scope.centerOnMe = function() {
            if(!$scope.map) {
                return;
            }
    
            $scope.loading = $ionicLoading.show({
              content: 'Getting current 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')
        };
    });
    

    Hope this helped!

    0 讨论(0)
  • 2020-12-31 12:54

    You can use the plugin angularjs-google-maps (ngmap) done for angularjs. It works for ionic. It gives you the ability to use google map v3.

    https://github.com/allenhwkim/angularjs-google-maps

    0 讨论(0)
  • 2020-12-31 13:02

    Check out this project on github

    http://angular-ui.github.io/angular-google-maps/#!/

    It uses Angular.js directives to provide APIs for a good part of the Google Maps functionality.

    0 讨论(0)
  • 2020-12-31 13:13

    I solved this by setting a in :

    <ion-content ng-controller="MapCtrl">
        <div id="map" data-tap-disabled="true"></div>
    </ion-content>
    

    and then you will have to do:

    .controller('MapCtrl', function($scope, $ionicLoading) {
    
        google.maps.event.addDomListener(window, 'load', function() {
            var newLatlng = new google.maps.LatLng(53.5000, -113.4300);
    
            var mapOptions = {
                center: newLatlng,
                zoom: 15,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
    
            var map = new google.maps.Map(document.getElementById("map"), mapOptions);
    
            navigator.geolocation.getCurrentPosition(function(pos) {
                map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
                var myLocation = new google.maps.Marker({
                    position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
                    map: map,
                    title: "My Location"
                });
            });
    
            $scope.map = map;
        });
    
    });
    

    Reference from: Implement Google Maps Using Ionic Framework

    0 讨论(0)
提交回复
热议问题