【问题标题】:AngularJS Google Maps Click Event not firing in ios devices ipad and iphoneAngularJS Google Maps Click 事件未在 ios 设备 ipad 和 iphone 中触发
【发布时间】:2018-07-17 11:04:41
【问题描述】:

我已将 google maps API 与我的应用程序集成以提供自动完成建议,它在我的笔记本电脑 (Ubuntu) 所有浏览器上也能正常工作。但是,最近我用 iPad 和 iPhone 进行了测试,它不起作用。我无法点击建议的地点。我正在使用 AngularJS,我的代码 sn-p 如下:

HTML

<div class="row">
    <div class="col-md-6">
        <form name="searchForm" ng-submit="fillInAddress()">
            <div class="input-group well">
                <input type="text" class="form-control" id="autocomplete" name="city" placeholder="Enter your city" 
                onFocus="geolocate()" required />
                <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit" ladda="ldloading.slide_left" data-style="slide-left" data-spinner-color="#000000"
                        ng-disabled="searchForm.$invalid">
                        <i class="fa fa-search"></i> Search
                    </button>
                </span>
            </div>
        </form>
    </div>
</div>

JS

 var placeSearch, autocomplete;

  function initAutocomplete() {
    autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')),
    {types: ['geocode']});
    autocomplete.addListener('place_changed', fillInAddress);
  }

  function fillInAddress() {
    // Get the place details from the autocomplete object.
    var place = autocomplete.getPlace();
    locationfilter.lat=place.geometry.location.lat();
    locationfilter.lon=place.geometry.location.lng();
    $scope.getOrders($scope.reportParams,locationfilter);
  }

  $scope.getOrders = function(pagination, locationfilter) {
    $scope.working = true;
    sliveorders
      .getLocOrders(pagination, locationfilter)
      .then(function(res) {
        $scope.Orders=res.data;
        $scope.totalItems=res.total;  
        $scope.working = false;
      })
      .catch(function(err) {
        console.log('Location search err:', err);
        $scope.working = false;
      });  
  }

  function geolocate() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var geolocation = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };
        var circle = new google.maps.Circle({
          center: geolocation,
          radius: position.coords.accuracy
        });
        autocomplete.setBounds(circle.getBounds());
      });
    }
  };
  initAutocomplete();  

【问题讨论】:

  • 你能创建一个小提琴或 plnkr 来重现你的问题吗? buttontype"submit" 不工作吗?意思是:ng-submit 不工作?
  • 是的,带有 type="submit" 的按钮不起作用记住它在笔记本电脑/台式机浏览器中工作它只是在 ipad、iphone 等 iOS 设备(笔记本电脑除外)上不起作用
  • 你能创建一个小提琴或 plnkr 来重现你的问题吗?我
  • 你在使用 ngTouch 吗?
  • @kannan 不,它适用于 AngularJS (ngmap.github.io) 和 Angular2 (github.com/ng2-ui/map),请检查一下

标签: javascript ios angularjs google-maps ipad


【解决方案1】:

place_changed 事件在 IOS 上受到阻碍。解决方法是将placed_changed 事件包装在一个普通的click 事件中,然后像这样分派place_changed 事件:

document.getElementById('autocomplete')
  .addEventListener('click', e=>autocomplete.dispatchEvent('place_changed'));

所以initAutoComplete() 变成:

  function initAutocomplete() {
    autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')),
    {types: ['geocode']});
    document.getElementById('autocomplete')
       .addEventListener('click', e=>autocomplete.dispatchEvent('place_changed'));
  }

【讨论】:

    【解决方案2】:

    这是一个使用 ng-map 的工作示例(在带有 IoS 9.2 的 iPad 上测试),这是一个用于 Google 地图的 AngularJS 库。

    angular.module("app", ['ngMap']).controller("MyCtrl", function($scope, NgMap) {
        var vm = this;
        //vm.types = "['geocode']";
        vm.types = "['establishment']";
        vm.usebounds = false;
        
        vm.placeChanged = function() {
            vm.place = this.getPlace();
            console.log('location', vm.place.geometry.location);
            vm.map.setCenter(vm.place.geometry.location);
        }
        
        vm.geolocate = function() {
          if (navigator.geolocation) {
          	console.log("geolocate");
            navigator.geolocation.getCurrentPosition(function(position) {
              var geolocation = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
              };
              console.log("position", position);
              if (vm.usebounds) {
                vm.mybounds = {
                  center: geolocation,
                  radius: position.coords.accuracy
                };
              }
              vm.map.setCenter(geolocation);  
            });
          }
        };
        
        NgMap.getMap().then(function(map) {
          	vm.map = map;
        });
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
    <script src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/build/scripts/ng-map.js"></script>
    
    <script src="https://maps.google.com/maps/api/js?libraries=places,visualization,drawing,geometry,places"></script>
    
    <div ng-app="app">
        <div ng-controller="MyCtrl as vm">
          Enter your city: <br/>
          <input places-auto-complete size=80
            ng-model="vm.address"
            component-restrictions="{country:'us'}"
            types="{{types}}"
            ng-focus="vm.geolocate()"
            strict-bounds="true"
            circle-bounds="{{vm.mybounds}}"
            on-place-changed="vm.placeChanged()" /> <br/>
         <input type="checkbox"
           ng-model="vm.usebounds">use bounds
            <br/>
          <div ng-show="vm.place">
            Address = {{vm.place.formatted_address}} <br/>
            Location: {{vm.place.geometry.location}}<br/>
          </div>
          <hr>
          bounds: {{vm.mybounds}}
        </div>
        <ng-map></ng-map>
    </div>

    这是一个 jsfiddle:https://jsfiddle.net/beaver71/mu5u71sg/

    【讨论】:

      猜你喜欢
      • 2014-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2023-03-07
      • 1970-01-01
      • 2014-02-07
      • 2019-08-18
      相关资源
      最近更新 更多