【问题标题】:Geo Location - Using Ionic Framework, AngularJS and Google API地理位置 - 使用 Ionic 框架、AngularJS 和 Google API
【发布时间】:2014-11-14 03:23:01
【问题描述】:

我们正在尝试在我们最新的Ionic Framework/AngularJS 项目中使用这个Codepen,但似乎无法解决这个问题。

我们希望能够点击“找到我们”并让Google Map Marker 显示我们当前的位置。

如果有人知道我们哪里出了问题,请告诉我们。 谢谢你。

// Google Map
.controller('MapCtrl', function($scope, $ionicLoading, $compile) {
  function initialise() {   
    var myLatlng = new google.maps.LatLng(53.068165,-4.076803);
    var mapOptions = {
        zoom: 15, 
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
      }
    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
    var marker = new google.maps.Marker({
      position: myLatlng,
      map: map,
    });
    $scope.map = map;    
  }
  google.maps.event.addDomListener(window, 'load', initialise);
  $scope.centerOnMe = function() {
    if(!$scope.map) {
      return;
    }
    $scope.loading = $ionicLoading.show({
      showBackdrop: true
    });
    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);
    });
  };
 });

【问题讨论】:

  • 您发布的 javascript/HTML 对我有用(显示标记)。问题必须在框架中。

标签: angularjs google-maps google-maps-api-3 ionic-framework


【解决方案1】:
when you find the current location of your phone first you find out the latitude and longitude.So First,Add the plugin your project 
1.cordova plugin add cordova-plugin-geolocation

2.module.controller('GeoCtrl', function($cordovaGeolocation,$http) {

  var posOptions = {timeout: 10000, enableHighAccuracy: false};
  $cordovaGeolocation
    .getCurrentPosition(posOptions)
    .then(function (position) {
      var lat  = position.coords.latitude //here you get latitude
      var long = position.coords.longitude //here you get the longitude
  $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&sensor=true').then(function(data){      $rootScope.CurrentLocation=data.data.results[0].formatted_address;//you get the current location here


        }, function(err) {
          // error
        });
    }, function(err) {
      // error
    });

}):

【讨论】:

    【解决方案2】:

    这是一个很好的例子。

    Codepen link

    .controller('MarkerRemoveCtrl', function($scope, $ionicLoading) {
    
      $scope.positions = [{
        lat: 43.07493,
        lng: -89.381388
      }];
    
      $scope.$on('mapInitialized', function(event, map) {
        $scope.map = map;
      });
    
      $scope.centerOnMe= function(){
    
        $ionicLoading.show({
          template: 'Loading...'
        });
    
    
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
          $scope.positions.push({lat: pos.k,lng: pos.B});
          console.log(pos);
          $scope.map.setCenter(pos);
          $ionicLoading.hide();
        });
    
      };
    
    });
    

    我确实使用了谷歌地图指令,只是为了将所有内容都保存在 angular-land 中。

    【讨论】:

    • 为什么navigator.geolocation.getCurrentPosition 在安卓模拟器中不起作用?
    • @evc 你找到安卓的解决方案了吗?
    • @EmptyData 可以,不用cordova插件,只要求地理定位权限,然后android会给浏览器原生的地理定位服务
    • @evc 如何在混合应用中请求权限?
    • 您至少需要 1 个插件,比如说相机,通过 cordova 添加插件 [名称] 安装它。会安装在plugins文件夹中,打开android.json,在camera权限后添加权限
    【解决方案3】:

    这是带有 Google 地图的 Ionic 应用的CodePen


    angular.module('ionic.example', ['ionic'])
    
        .controller('MapCtrl', function($scope, $ionicLoading, $compile) {
          function initialize() {
            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')
          };
    
        });
    

    【讨论】:

    • 请在问题本身中发布相关代码,而不仅仅是指向外部资源的链接。
    • @geocodezip 在 CodePen 中,所有应用程序都在运行,但我已经编辑了答案,您可以看到 MapCtrl
    猜你喜欢
    • 2023-04-10
    • 2016-07-16
    • 1970-01-01
    • 2013-04-23
    • 2018-08-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多