Ionic 项目中 android 的位置

location in Ionic project for android

在一个简单的 Ionic 应用程序中,我必须在地图上获取当前位置。当我点击“找到我”时,它在浏览器中工作正常,但它在实际 Android 设备上不起作用。

我正在使用以下代码

View.html

            <ion-view view-title="{{navTitle}}">
             <ion-content>
                    <div id="map" data-tap-disabled="true"></div>
            </ion-content>
             <ion-footer-bar class="bar-stable">
                <a ng-click="centerOnMe()" class="button button-icon icon ion-navigate">Find Me</a>
             </ion-footer-bar>
            </ion-view>

controllers.js

        .controller('googlemap', function($scope, $ionicLoading, $compile) {
               $scope.navTitle = 'Google Map';
              $scope.$on('$ionicView.afterEnter', function(){
            if ( angular.isDefined( $scope.map ) ) {
                google.maps.event.trigger($scope.map, 'resize');
            }
          });

          function initialize() {
                //var myLatlng = new google.maps.LatLng(43.07493,-89.381388);
                var myLatlng = new google.maps.LatLng(18.520430300000000000,73.856743699999920000);
                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: 'Pune(India)'
                });

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

                $scope.map = map;
              }
              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')
              };

        })

也在我的设备上的设备位置上,但我仍然 problem.I 在我的屏幕上长时间显示以下输出。

直接使用 ngCordova $cordovaGeoLocationPlugin 不是更容易吗?您可以从此插件(纬度和经度)获取位置,然后将其传递给 GoogleAPI。我认为那样会容易得多。

ngCordova geoLocationPlugin

只是一个建议。

如果您在打开 GPS 的情况下启动应用程序,则此示例非常适用。但是从您设备中的 GPS 关闭开始,然后进入应用程序并选择 GPS 打开,功能 returns 总是 GPS 超时错误 = 3。有什么解决方案吗?
我在这里发布了这个错误: When I start the App with GPS off , and later GPS is activated, Location is not detected