gpt4 book ai didi

javascript - ionic : click event taking time for selecting places from Google Places api

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:23 25 4
gpt4 key购买 nike

我是 IONIC 的新手。我正在尝试在我的 ionic 应用程序中使用 google places api。我可以通过 Google Places api 获取这些地方。但问题是我必须长按应用中的自动完成选项,否则选择不会反射(reflect)在应用中。

这是我的 Html 代码。

<script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"></script>    
<input id="pac-input" class="controls" type="text" placeholder="Search Box" data-tap-disabled="true">
<div id="map" data-tap-disabled="true"></div>

这是我的 MapController.js

     .controller('MapCtrl', function($scope, $rootScope, $state, $cordovaGeolocation, $ionicLoading, Markers, $ionicPopup, $location, SearchAll, shareData, constants) {        
initAutocomplete();

function initAutocomplete() {
console.log("in Map Autocomplete");
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 19.075984, lng: 72.877656 },
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});

// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
// map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});

var markers = [];
// [START region_getplaces]
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();

console.log("in searchBox Listner");

if (places.length == 0) {
return;
}

console.log("Places : ", places);
console.log("LAT : ", places[0].geometry.location.lat());
console.log("LAONG : ", places[0].geometry.location.lng());
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];

// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};

// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));

if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
// [END region_getplaces]
}
});

最佳答案

此问题的解决方案在 https://github.com/driftyco/ionic/issues/1798 中讨论。 .问题是 google 会动态添加需要 data-tap-disabled 属性的元素,因此您必须在 google 将这些元素添加到 dom 后手动添加该属性。

$scope.disableTap = function () {
var container = document.getElementsByClassName('pac-container');
angular.element(container).attr('data-tap-disabled', 'true');
var backdrop = document.getElementsByClassName('backdrop');
angular.element(backdrop).attr('data-tap-disabled', 'true');
// leave input field if google-address-entry is selected
angular.element(container).on("click", function () {
document.getElementById('pac-input').blur();
});
};

现在将 ng-change='disableTap()' 添加到您的输入:

<input ng-change='disableTap()' id="pac-input" class="controls" type="text" placeholder="Search Box" data-tap-disabled="true">

github 链接使用 ng-focusng-change 对我来说效果更好,因为有时在触发焦点时生成的元素还不存在。

关于javascript - ionic : click event taking time for selecting places from Google Places api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35442720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com