简体   繁体   English

位于Ionic项目中的android

[英]location in Ionic project for android

In a simple Ionic app I have to get current location on map. 在一个简单的离子应用程序中,我必须在地图上获取当前位置。 It's works fine in browser when i click find me, but it's not working on actual Android device. 当我点击找到我时,它在浏览器中工作正常,但它不适用于实际的Android设备。

I'm using the following code 我正在使用以下代码

View.html 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 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')
              };

        })

Also in my device on the device Location but still I have problem.I got following output on my screen for long time. 另外在我的设备上设备位置,但我仍然有问题。我在屏幕上长时间得到以下输出。

在此输入图像描述

Isn't it easier to just use the ngCordova $cordovaGeoLocationPlugin ? 使用ngCordova $cordovaGeoLocationPlugin不是更容易吗? You can get the position from this plugin ( latitude and longitude) and then pass this in GoogleAPI. 您可以从此插件获取位置(纬度和经度),然后在GoogleAPI中传递此位置。 I think it will be lot easier that way. 我认为这样会更容易。

ngCordova geoLocationPlugin ngCordova geoLocationPlugin

Just a suggestion. 只是一个建议。

This example works perfect in case you start the app with GPS on. 此示例非常适用于启动GPS的应用程序。 But starting with GPS off in you device, and after that, entering in the app and selecting GPS on, the function returns always GPS timeout ERROR = 3. Any solution for that? 但是在您的设备中关闭GPS开始,之后,进入应用程序并选择GPS开启,该功能始终返回GPS超时ERROR = 3.任何解决方案?
I have posted this error here: When I start the App with GPS off , and later GPS is activated, Location is not detected 我在这里发布了这个错误: 当我关闭GPS启动应用程序,以后激活GPS时,未检测到位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM