我正在使用googles api创建应用。经过几个小时的游戏,我注意到我最大限度地搜索了地方搜索API。
我希望能够搜索地址并将其添加到地图中。我这样做错了吗?或者为什么我已经创建了这么多请求? 是否可以在其他地方获取地址?
我知道很难说我是否做错了,但仅仅通过请求的外观我认为你可能会说我的代码正在为许多请求做好准备。
这是我使用的代码:
$scope.initiateMap = function () {
$scope.map = new google.maps.Map(document.getElementById('map'), {
zoom: 11
});
//var marker = new google.maps.Marker({
// position: uluru,
// map: map
//});
navigator.geolocation.getCurrentPosition(function (position) {
var initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
$scope.map.setCenter(initialLocation);
}, function () {
console.log('user denied request for position');
});
var input = document.getElementById('address-field');
var searchBox = new google.maps.places.SearchBox(input);
//map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
$scope.map.addListener('bounds_changed', function () {
searchBox.setBounds($scope.map.getBounds());
});
var markers = [];
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
//var address_autocomplete = new google.maps.places.Autocomplete(address_input, { types: ['address'] });
//if (places.length == 0) {
// return;
//}
// 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) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
if (place.types[0] !== "street_address") {
alert("not a street address");
markers = [];
markers.forEach(function (marker) {
marker.setMap(null);
});
return;
}
$scope.apartment.Address.Longitude = place.geometry.location.lng();
$scope.apartment.Address.Latitude = place.geometry.location.lat();
$scope.apartment.Address.AddressName = place.formatted_address;
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: $scope.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);
}
});
$scope.map.fitBounds(bounds);
});
}
答案 0 :(得分:0)
正如Google指南中所述,您可以通过在Google API控制台上启用结算功能,免费提高请求限额,每24小时最多可增加150,000个请求。这只是为了验证您的身份,因此您的卡不会被收费。
再看here。