gpt4 book ai didi

javascript - 如何通过自动完成在 Angular 谷歌地图上放置标记?

转载 作者:行者123 更新时间:2023-12-03 08:41:27 25 4
gpt4 key购买 nike

我想通过自动完成搜索在谷歌 Angular map 上放置标记,我正在以纬度和经度的形式获取位置,但标记没有添加,请检查......

Controller 代码为

$timeout(function() {

var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);

google.maps.event.addListener(searchBox, 'places_changed', function() {
var marker= [] ;
var places = searchBox.getPlaces();

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

var loc_data = {

coords: {
latitude: places[0].geometry.location.lat(),
longitude: places[0].geometry.location.lng()
}
};
marker.push(loc_data);
$scope.markers=marker;
});

HTML代码是

    <ui-gmap-markers models="markers" idkey="markers.id" coords="'coords'" >

</ui-gmap-markers>

<div class="col-md-4">

<input id="pac-input" class="controls" type="text" placeholder="Search Box">
</div>

最佳答案

 <div class="form-group">
<label for="pacinput" class="control-label col-md-4">Search Location</label>
<div class="col-md-8">
<input id="pacinput" class="form-control" type="text" placeholder="Search Location">
</div>
</div>

<div class="form-group">
<div class="col-md-offset-4 col-md-8">
<asp:HiddenField runat="server" ID="address" />
<asp:HiddenField ID="txtLat" runat="server" />
<asp:HiddenField ID="txtLng" runat="server" />
<div id="map" style="width: 100%; height: 380px;">
</div>
</div>
</div>

//此示例使用 Google 地点自动完成功能向 map 添加搜索框 //特征。人们可以输入地理搜索。搜索框将返回一个 //选择包含地点和预测搜索词混合的列表。

    // This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 15.362813, lng: 75.126129 },
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
});

// Create the search box and link it to the UI element.
var input = document.getElementById('pacinput');
var searchBox = new google.maps.places.SearchBox(input);

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

var markers = [];
// 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();

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) {
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);
});
}

</script>

关于javascript - 如何通过自动完成在 Angular 谷歌地图上放置标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038215/

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