一次只允许添加一个标记

发布于 2024-11-16 11:05:35 字数 3486 浏览 1 评论 0原文

我想知道是否有人可以帮助我解决我希望的简单问题。

我使用下面的代码,允许用户在输入表单上输入地址,单击对地址进行地理编码的按钮,返回纬度/经度坐标,在地图上放置标记。如果用户随后添加另一个地址的详细信息并单击该按钮,代码会对该地址进行地理编码并在地图上放置另一个标记,换句话说,地图上现在有两个标记。

也许有人可以告诉我如何对我的代码进行更改,在用户单击“保存”按钮之前,任何时候只允许一个标记可编辑。即,如果用户输入伦敦地址,则会像以前一样进行地理编码,但是当他们将地址更改为爱丁堡时,该标记会移动到新位置,因此地图上会出现一个标记,直到他们单击“保存”按钮,该按钮将被清除我表单上的字段。

function Geocode() {

    // This is defining the global variables
    var map, geocoder, myMarker;

    window.onload = function() {

    //This is creating the map with the desired options
    var myOptions = {
    zoom: 5,
    center: new google.maps.LatLng(55.378051,-3.435973),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    mapTypeControl: true,
    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.TOP_LEFT
    },
    navigationControl: true,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.ZOOM_PAN,
        position: google.maps.ControlPosition.TOP_RIGHT
    },
    scaleControl: true,
    scaleControlOptions: {
        position: google.maps.ControlPosition.BOTTOM_LEFT
    }
    };

    map = new google.maps.Map(document.getElementById('map'), myOptions);

    // This is making the link with the 'Search For Location' HTML form
    var form = document.getElementById('SearchForLocationForm');

    // This is catching the forms submit event
    form.onsubmit = function() {

    // This is getting the Address from the HTML forms 'Address' text box
    var address = document.getElementById('Address').value;

    // This is making the Geocoder call
    getCoordinates(address);

    // This is preventing the form from doing a page submit
    return false;
    }
    }

    // This creates the function that will return the coordinates for the address
    function getCoordinates(address) {

    // This checks to see if there is already a geocoded object. If not, it creates one
    if(!geocoder) {
    geocoder = new google.maps.Geocoder();
    }

    // This is creating a GeocoderRequest object
    var geocoderRequest = {
    address: address
    }

    // This is making the Geocode request
    geocoder.geocode(geocoderRequest, function(results, status) {

    // This checks to see if the Status is 'OK 'before proceeding
    if (status == google.maps.GeocoderStatus.OK) {

    // This centres the map on the returned location
    map.setCenter(results[0].geometry.location);

    // This creates a new marker and adds it to the map
    var myMarker = new google.maps.Marker({
        position: results[0].geometry.location,
        draggable:true
    });

    //This fills out the 'Latitude' and 'Longitude' text boxes on the HTML form
    document.getElementById('Latitude').value=  results[0].geometry.location.lat();
    document.getElementById('Longitude').value=  results[0].geometry.location.lng();

    //This allows the marker to be draggable and tells the 'Latitude' and 'Longitude' text boxes on the HTML form to update with the new co-ordinates as the marker is dragged
    google.maps.event.addListener(     
    myMarker,     
    'dragend',     
    function() {         
    document.getElementById('Latitude').value = myMarker.position.lat();         
    document.getElementById('Longitude').value = myMarker.position.lng(); 

    var point = myMarker.getPosition();
    map.panTo(point);   
    } 
    ); 
    }
    } 
    )
    }
    })();

I wonder whether someone could possibly please help me with what I hope is a simple fix.

I am using the code below, to allow a user to input an address on an input form, click a button which geocodes the address, returning the Lat/Lng co-ordinates, placing a marker on the map. If the user then add details of another address and clicks the button, the code geocodes that address and places another marker on the map, in other words there are now two markers on the map.

Could someone perhaps tell me how I would go about making changes to my code that would only allow only one marker to be editable at any one time until the user clicks a 'save' button. i.e. If the user types in the address as London, it is geocoded as before, but when they change the address to say Edinburgh that marker moves to the new location, hence one marker on the map until they click the save button which will then clear the fields on my form.

function Geocode() {

    // This is defining the global variables
    var map, geocoder, myMarker;

    window.onload = function() {

    //This is creating the map with the desired options
    var myOptions = {
    zoom: 5,
    center: new google.maps.LatLng(55.378051,-3.435973),
    mapTypeId: google.maps.MapTypeId.TERRAIN,
    mapTypeControl: true,
    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.TOP_LEFT
    },
    navigationControl: true,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.ZOOM_PAN,
        position: google.maps.ControlPosition.TOP_RIGHT
    },
    scaleControl: true,
    scaleControlOptions: {
        position: google.maps.ControlPosition.BOTTOM_LEFT
    }
    };

    map = new google.maps.Map(document.getElementById('map'), myOptions);

    // This is making the link with the 'Search For Location' HTML form
    var form = document.getElementById('SearchForLocationForm');

    // This is catching the forms submit event
    form.onsubmit = function() {

    // This is getting the Address from the HTML forms 'Address' text box
    var address = document.getElementById('Address').value;

    // This is making the Geocoder call
    getCoordinates(address);

    // This is preventing the form from doing a page submit
    return false;
    }
    }

    // This creates the function that will return the coordinates for the address
    function getCoordinates(address) {

    // This checks to see if there is already a geocoded object. If not, it creates one
    if(!geocoder) {
    geocoder = new google.maps.Geocoder();
    }

    // This is creating a GeocoderRequest object
    var geocoderRequest = {
    address: address
    }

    // This is making the Geocode request
    geocoder.geocode(geocoderRequest, function(results, status) {

    // This checks to see if the Status is 'OK 'before proceeding
    if (status == google.maps.GeocoderStatus.OK) {

    // This centres the map on the returned location
    map.setCenter(results[0].geometry.location);

    // This creates a new marker and adds it to the map
    var myMarker = new google.maps.Marker({
        position: results[0].geometry.location,
        draggable:true
    });

    //This fills out the 'Latitude' and 'Longitude' text boxes on the HTML form
    document.getElementById('Latitude').value=  results[0].geometry.location.lat();
    document.getElementById('Longitude').value=  results[0].geometry.location.lng();

    //This allows the marker to be draggable and tells the 'Latitude' and 'Longitude' text boxes on the HTML form to update with the new co-ordinates as the marker is dragged
    google.maps.event.addListener(     
    myMarker,     
    'dragend',     
    function() {         
    document.getElementById('Latitude').value = myMarker.position.lat();         
    document.getElementById('Longitude').value = myMarker.position.lng(); 

    var point = myMarker.getPosition();
    map.panTo(point);   
    } 
    ); 
    }
    } 
    )
    }
    })();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

和影子一齐双人舞 2024-11-23 11:05:35

执行此操作的一个简单方法是将当前可编辑标记存储在全局变量中。每当用户按下“保存”时,您就可以确保用户无法再对其进行编辑。

需要记住的一件好事是,如果您将来想再次编辑任何这些标记,则需要以某种方式存储它们。数组是执行此操作的正常方法。所以,换句话说。您要做的是:

用户输入位置。地理编码返回结果。使用该位置创建一个新标记并将其存储在全局变量中。如果用户更改位置,您将更改该标记上的位置。如果用户按下“保存”,您将清除该变量。当用户想要创建新标记时,您只需将全局变量分配给标记的新实例即可。

如果您想再次访问“已保存”标记,请记住将它们保存在数组中。如果您不这样做,标记仍然存在并显示在地图上,但您无法访问它们。

希望有帮助。

A simple way to do this would be to store the currently editable marker in a global variable. Whenever the user presses save, you would then make sure the user could not edit it anymore.

A good thing to remember is that if you ever want to edit any of those markers again in the future you would need to store them somehow. An Array is the normal way to do this. So, in other words. What you would do is:

User enters location. Geocode returns a result. Make a new marker with that location and store it in a global variable. If the user changes the location, you change the location on that marker. If the user presses save, you clear the variable. When the user wants to make a new marker you simply assign the global variable to a new instance of a marker.

And remember to save the "saved" markers in an array if ever you want to access them again. If you don't do that, the markers will still exist and be shown on the map, but you have no way of accessing them.

Hope that helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文