gpt4 book ai didi

php - 如何允许访问者向 map 添加任何位置以及如何将其保存在数据库中

转载 作者:行者123 更新时间:2023-11-30 17:56:17 26 4
gpt4 key购买 nike

实际上,我在一个网站上工作,用户可以在其中添加他的业务。我想给他一个在 map 上添加任何位置的功能。当他将他的业务详细信息与 map 详细信息一起保存到数据库时,这样在网站上发布他的业务后,它应该在 map 上显示该位置。简而言之,我想允许用户在 map 上添加他的位置,并且在特定用户 map 的网站上应该在 map 上显示他的位置。 由于我对这一切都不熟悉,所以我很困惑如何做到这一点。所以请帮帮我。我在谷歌上搜索了它,但我的问题没有得到任何帮助。提前致谢....

这是我试过的代码:

        <script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-33.8688, 151.2195),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var input = /** @type {HTMLInputElement} */(document.getElementById('searchTextField'));
var autocomplete = new google.maps.places.Autocomplete(input);

autocomplete.bindTo('bounds', map);

var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});

google.maps.event.addListener(autocomplete, 'place_changed', function() {
infowindow.close();
marker.setVisible(false);
input.className = '';
var place = autocomplete.getPlace();
if (!place.geometry) {
// Inform the user that the place was not found and return.
input.className = 'notfound';
return;
}

// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker.setIcon(/** @type {google.maps.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(35, 35)
}));
marker.setPosition(place.geometry.location);
marker.setVisible(true);

var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
var place = autocomplete.getPlace();
document.getElementById('city2').value = place.name;
document.getElementById('cityLat').value = place.geometry.location.lat();
document.getElementById('cityLng').value = place.geometry.location.lng();
var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
});

// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener(id, types) {
var radioButton = document.getElementById(id);
google.maps.event.addDomListener(radioButton, 'click', function() {
autocomplete.setTypes(types);
});
}

setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
<style>
#map-canvas, #map_canvas {
height: 400px;
width: 900px;
}

@media print {
#map-canvas, #map_canvas {
height: 400px;
width: 900px;
}
}

</style>
</head>
<body>
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File: function.map.php
* Type: map
* Name: Map
* Purpose: Accept the keyword & find it on a map.
* -------------------------------------------------------------
*/

function smarty_function_map($params, &$smarty)
{
?>

<div id="panel">
<label>Location/Area*</label><input id="searchTextField" type="text" size="50" placeholder="Enter Location/Area">
<input type="text" id="city2" name="city2" />
<input type="text" id="cityLat" name="cityLat" />
<input type="text" id="cityLng" name="cityLng" />
</div>
<div id="map-canvas"></div>

<?php

}
?>
</body>
</html>

最佳答案

要允许用户通过点击向 map 添加标记,您需要设置一个事件监听器,该监听器将监听 map 上的任何点击.示例:

var marker;

google.maps.event.addListener(map, 'click', function(event) {
if ( marker ) {
marker.setPosition(event.latLng);
} else {
marker = new google.maps.Marker({
position: event.latLng,
map: map,
draggable:true
});
}
});

您还希望允许用户在绘制标记后拖动标记。您可以通过将标记设置为可拖动来执行此操作(我在上面的示例中这样做了)。您还需要通过将事件监听器附加到相关标记来监听任何更改:

google.maps.event.addListener(marker, 'dragend', function(){
//Do something because marker has been dragged somewhere...
});

最后,每次点击 map 或拖动标记时,您都需要将生成的纬度和经度值保存在隐藏字段中,以便您可以将位置保存在数据库中。

关于php - 如何允许访问者向 map 添加任何位置以及如何将其保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993112/

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