gpt4 book ai didi

javascript - 解决交互式 map 中的 "undefined"自定义标记

转载 作者:行者123 更新时间:2023-11-29 15:33:09 25 4
gpt4 key购买 nike

我正在尝试使用自定义标记创建交互式 map ,例如:http://cliffcloud.github.io/Leaflet.LocationShare/

当您单击左上角的标记时,该标记会显示在 map 中,您可以在 URL 链接中创建一条消息并进行共享。我的问题是:如果您不发送任何消息。标记将显示“未定义”但是,我找不到控制消息的代码,我可以在其中将“未定义”更改为默认消息。任何人都知道您可以在代码的哪一部分更改“未定义”消息。自定义标记的代码如下:

L.LocShare = {}
var LS = L.LocShare
LS.Send = {}
LS.Send.Marker = {}
LS.Send.Popup = L.popup().setContent('<div><input id="sendText" type="text" style="border-color:#a7a7a7;border:solid;border-width:2px;border-radius:5px;height:30px;" size="30" onkeyup="L.LocShare.Send.UpdateMessage( this )" placeholder="enter your message"/></div><div style="height:35px;"><button style="border-style:solid;border-radius:5px;border-color:#3d94f6;float:right;color:white;background-color:#3d94f6;height:35px;font-size:15px;line-height:3px;margin:5px;" onclick="copyPrompt()">get url</button></div></div>')
LS.Receive = {}
LS.Receive.Marker = {}
LS.Receive.Popup = L.popup()
var sendIcon = L.icon({
iconUrl: "https://raw.githubusercontent.com/CliffCloud/Leaflet.LocationShare/master/dist/images/IconMapSend.png",
iconSize: [50, 50], // size of the icon
iconAnchor: [25, 50], // point of the icon which will correspond to marker's location
popupAnchor: [0, -30] // point from which the popup should open relative to the iconAnchor
})

receiveIcon = L.icon({
iconUrl: "https://raw.githubusercontent.com/CliffCloud/Leaflet.LocationShare/master/dist/images/IconMapReceive.png",
iconSize: [50, 50], // size of the icon
iconAnchor: [25, 50], // point of the icon which will correspond to marker's location
popupAnchor: [0, -30] // point from which the popup should open relative to the iconAnchor
})

L.Map.addInitHook(function () {
this.sharelocationControl = new L.Control.ShareLocation();
this.addControl(this.sharelocationControl);
this.whenReady( function(){
populateMarker(this);
})
});

L.Control.ShareLocation = L.Control.extend({
options: {
position: 'topleft',
title: 'Share Location'
},

onAdd: function () {
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control');

this.link = L.DomUtil.create('a', 'leaflet-bar-part', container);
// var userIcon = L.DomUtil.create('i', 'fa fa-users fa-lg', this.link);
var userIcon = L.DomUtil.create('img' , 'img-responsive' , this.link);
userIcon.src = 'https://raw.githubusercontent.com/CliffCloud/Leaflet.LocationShare/master/dist/images/IconLocShare.png'
this.link.href = '#';

L.DomEvent.on(this.link, 'click', this._click, this);

return container;
},

_click: function (e) {
L.DomEvent.stopPropagation(e);
L.DomEvent.preventDefault(e);
// TODO: get location and putout url
placeMarker( this._map )
},
});

populateMarker = function (selectedMap) {
// replace the line below with the results of any Url parser
var intermediate = getJsonFromUrl()
if ( isFinite(intermediate.lat) && isFinite(intermediate.lng) ){
LS.Receive.message = intermediate.M
LS.Receive.lat = + intermediate.lat
console.log( intermediate.lat )
LS.Receive.lng = + intermediate.lng
console.log( intermediate.lng )
var text = '<table><tr><td><p>' + LS.Receive.message + '</p></td><td><p>Lat: ' + LS.Receive.lat + '</p><p>Lng: ' + LS.Receive.lng + '</p></td></tr></table>'
// LS.Receive.Popup.setContent(LS.Receive.message)
LS.Receive.Marker = L.marker( [ LS.Receive.lat , LS.Receive.lng] , {icon:receiveIcon})
console.log( LS.Receive.Marker._latlng )
LS.Receive.Marker.bindPopup(LS.Receive.message)
LS.Receive.Marker.addTo(selectedMap)
LS.Receive.Marker.openPopup()
}
}

function getJsonFromUrl () {
var params = {}
params.query = location.search.substr(1);
params.parsed = decodeURIComponent( params.query )
params.data = params.parsed.split("&");
params.result = {};
for(var i=0; i<params.data.length; i++) {
var item = params.data[i].split("=");
params.result[item[0]] = item[1];
}
// This will return all of the data in the query parameters in object form
// getJsonFromUrl() only splits on ampersand and equals -- jquery can do better
// But so could you!! submit a pull request if you've got one!
return params.result;
}


function copyPrompt() {
window.prompt("Send this location with: Ctrl+C, Enter", '' +
location.origin + location.pathname + '?' +
'lat' + '=' + LS.Send.lat + '&' +
'lng' + '=' + LS.Send.lng + '&' +
'M' + '=' + LS.Send.Message);
}

function placeMarker( selectedMap ){
// var test = LS.Send.Marker._latlng
// if ( isFinite(test.lat) && isFinite(test.lng) ){
if (!LS.Send.Marker._latlng ) {
console.log('if (!LS.Send.Marker._latlng ) { passed! line 95')
LS.Send.Marker = L.marker( selectedMap.getCenter() , {draggable: true,icon: sendIcon} );
setSendValues( selectedMap.getCenter() )
LS.Send.Marker.on('dragend', function(event) {
setSendValues( event.target.getLatLng());
LS.Send.Marker.openPopup();
});
LS.Send.Marker.bindPopup(LS.Send.Popup);
LS.Send.Marker.addTo(selectedMap);
} else {
LS.Send.Marker.setLatLng( selectedMap.getCenter() )
}
//selectedMap.setView( location , 16 )
LS.Send.Marker.openPopup();
// }
};

LS.Send.UpdateMessage = function( text ){
var encodedForUrl = encodeURIComponent( text.value );
LS.Send.Message = encodedForUrl
}

function setSendValues( result ){
LS.Send.lat = result.lat;
LS.Send.lng = result.lng;
}

最佳答案

如果您查看该按钮,您可以看到它具有 copyPrompt() 的 onclick,因此请检查该函数...它正在那里创建 URL ...&M={LS .Send.Message.因此,您需要做的就是检查它是否已定义,如果没有,则设置默认值。

function copyPrompt() {
if (typeof LS.Send.Message === 'undefined') LS.Send.Message = 'Default';
window.prompt("Send this location with: Ctrl+C, Enter", '' +
location.origin + location.pathname + '?' +
'lat' + '=' + LS.Send.lat + '&' +
'lng' + '=' + LS.Send.lng + '&' +
'M' + '=' + LS.Send.Message);
}

关于javascript - 解决交互式 map 中的 "undefined"自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32443831/

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