我正在尝试使用自定义标记创建交互式地图,例如:http://cliffcloud.github.io/Leaflet.LocationShare/
当您单击左上角的标记时,标记会显示在地图中,您可以在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;
}
答案 0 :(得分:1)
如果您查看按钮,则可以看到它已点击copyPrompt()
,因此请检查该功能...它正在...&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);
}