谷歌地图 API v3 标记问题
Google maps API v3 marker issues
我正在尝试编写一个地图,您可以在其中搜索地址,它会显示一个包含自动完成项目的列表,当您单击它时,它会将标记带到地址。如果需要,只需拖动标记即可,而不使用地址搜索。
我正在使用 v2 并成功使用它 - 我可以获取纬度和 lng 值并将它们插入到隐藏输入中,所以以后我可以将它们插入数据库。
使用 V3,我能做的最好的事情就是显示地图并使搜索工作,但我可以将纬度和 lng 插入到隐藏的输入中,并且我不能让唯一的标记更改位置。
如果我创建一个标记以从文档开始,当您搜索并单击时,它将创建另一个标记。
这是我的代码:
function initialize() {
var myLatlng = new google.maps.LatLng(-1.456688, -48.477586);
var mapOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var markers = [];
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
draggable: true,
title: 'Hello World!'
});
// Create the search box and link it to the UI element.
var input = /** @type {HTMLInputElement} */(
document.getElementById('pac-input'));
var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */(input));
// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}
// For each place, get the icon, place name, and location.
markers = [];
var bounds = new google.maps.LatLngBounds();
for (var i = 0, place; place = places[i]; i++) {
var image = {
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.
var marker = new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location,
draggable: true
});
markers.push(marker);
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
});
// Bias the SearchBox results towards places that are within the bounds of the
// current map's viewport.
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
我很抱歉问了这么多,但我真的把一整天都花在了 API 文档上,无法让它变得更好。
搜索框不适合搜索
地址,最好使用Places-Autocomplete。
要更改 ONLY 标记的位置,您只需设置标记的位置属性。
function initialize() {
var myLatlng = new google.maps.LatLng(-1.456688, -48.477586);
var mapOptions = {
zoom: 14,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
draggable: true,
title: 'Hello World!'
});
//set the value of the hidden inputs when the position changes
google.maps.event.addListener(marker, 'position_changed', function() {
document.getElementById('latitude').value = this.getPosition().lat();
document.getElementById('longitude').value = this.getPosition().lng();
});
// Create an Autocomplete and link it to the UI element.
var input = /** @type {HTMLInputElement} */ (
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
var searchBox = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */
(input), {
types: ['geocode']
});
// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'place_changed', function() {
var place = this.getPlace();
//when place has been found
if (place.geometry) {
marker.setOptions({
title: place.name,
position: place.geometry.location
});
if (place.geometry.viewport) {
marker.getMap().fitBounds(place.geometry.viewport);
} else {
marker.getMap().setCenter(place.geometry.location);
}
}
//otherwise
else {
marker.setOptions({
title: null
});
alert('place not found');
}
});
// Bias the SearchBox results towards places that are within the bounds of the
// current map's viewport.
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
searchBox.setBounds(bounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0
}
<script src="//maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>
<input id="pac-input"/>
<input type="hidden" id="latitude"/>
<input type="hidden" id="longitude"/>
<div id="map"></div>
相关文章:
- Google 日历 API V3 会解决与会者和创建者 Java 脚本问题
- 谷歌地图API v3 addDomListener方法问题
- 谷歌地图 API v3 标记问题
- Slim Framework v3 - PUT 路由的问题
- JSONP 和 Github API v3 的问题
- 通过https在Firefox中加载Google 地图api V3时出现问题
- 谷歌地图API v3:我在热图上显示我的数据时遇到问题
- 让地图在谷歌地图 API v3 上工作的问题
- 自定义谷歌地图 v3 控制位置问题
- 谷歌地图API v3-Ajax(Javascript+Jquery)GET问题
- Google API v3中的标记出现问题
- 谷歌地图v3:标记和多段线未渲染.我的代码出了什么问题
- 谷歌地图V3搜索问题
- 谷歌地图v3与MeteorJS加载同步问题
- 谷歌地图javascript API v3西班牙语问题
- 谷歌地图Javascript V3.21显示geoJSON功能时出现问题
- 在Biostall-google-maps-V3 API库中显示带有latlong json数据的标记问题
- 在Google Maps v3上显示一系列标记时出现问题
- Google API V3:如果还定义了groundOverlay,则多边形鼠标悬停事件的问题
- 在Google Maps API v3中显示多个标记和方向变量的infoWindows问题