マーカーを 1 つだけ表示するマップを Google マップで作成しています。ユーザーが初めてマップをクリックすると、マーカーが作成されます (その周りに円が表示されます)。ユーザーがもう一度クリックすると、古いマーカー (および円) が新しい場所に移動します。
基本的に、これはマーカーと円を移動するか、それらを削除して新しいマーカーと円を作成することによって行われます。
現時点では後者の方法を使用しようとしていますが、残念ながらうまくいきません。誰でも助けてもらえますか?
<script>
//Initial variables (map and the markers array, which we're going to try to get rid of)
var map;
var markersArray = [];
//and GO!
function initialize()
{
//need to correct our starting location - over UK would be nice
var haightAshbury = new google.maps.LatLng(37.7699298, -122.4469157);
var mapOptions =
{
zoom: 12,
center: haightAshbury,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
//listen out for clicks and, when clicked, add a marker
google.maps.event.addListener(map, 'click', function(event)
{
addMarker(event.latLng);
});
}
// Add a marker to the map and push to the array.
function addMarker(location)
{
markersArray = [];
setAllMap(null);
var marker = new google.maps.Marker(
{
position: location,
map: map,
draggable: true
});
//create a circle
var circle = new google.maps.Circle(
{
map: map,
radius: 3000
});
//bind the circle to the marker we've also just created
circle.bindTo('center', marker, 'position');
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>