Javascript 是否使用mapbox将我的GeoJSON提要中的图像包含到工具提示中?
我正在尝试将GeoJSON提要中的图像显示到工具提示中 我引用的脚本在工具提示中显示自定义图像(但不是来自geojson提要)。所以我试图将两者结合起来,但我不知道该怎么办。一直在阅读mapbox和JS API文档 任何建议或解决方案都将不胜感激 我所拥有的:Javascript 是否使用mapbox将我的GeoJSON提要中的图像包含到工具提示中?,javascript,leaflet,geojson,mapbox,Javascript,Leaflet,Geojson,Mapbox,我正在尝试将GeoJSON提要中的图像显示到工具提示中 我引用的脚本在工具提示中显示自定义图像(但不是来自geojson提要)。所以我试图将两者结合起来,但我不知道该怎么办。一直在阅读mapbox和JS API文档 任何建议或解决方案都将不胜感激 我所拥有的: // for clickable coords // var latitude = document.getElementById('latitude'); var longitude = document.getElementById
// for clickable coords //
var latitude = document.getElementById('latitude');
var longitude = document.getElementById('longitude');
// dark map example + set views //
var map = L.mapbox.map('map', 'examples.map-y7l23tes')
.setView([40.70319876407339, -74.03961181640625], 12);
L.control.locate().addTo(map);
// click to add coords //
map.on('click', function(e) {
window[e.type].innerHTML = e.containerPoint.toString() + ', ' + e.latlng.lng.toString()+ ' XsupX ' + e.latlng.lat.toString();
latitude.value = e.latlng.lat.toString();
longitude.value = e.latlng.lng.toString();
var click = document.getElementById('click'); });
// grip JSON //
var featureLayer = L.mapbox.featureLayer()
.loadURL('/locations.json')
.addTo(map);
我引用的脚本包含自定义图像,但没有GeoJSON提要作为图像源
var map = L.mapbox.map('map', 'examples.map-9ijuk24y');
var geoJson = [{
type: 'Feature',
"geometry": { "type": "Point", "coordinates": [-77.03, 38.90]},
"properties": {
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Cherry_Blossoms_and_Washington_Monument.jpg/320px-Cherry_Blossoms_and_Washington_Monument.jpg",
"url": "https://en.wikipedia.org/wiki/Washington,_D.C.",
"marker-symbol": "star",
"city": "Washington, D.C."
}
}, {
type: 'Feature',
"geometry": { "type": "Point", "coordinates": [-87.63, 41.88]},
"properties": {
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Chicago_sunrise_1.jpg/640px-Chicago_sunrise_1.jpg",
"url": "https://en.wikipedia.org/wiki/Chicago",
"city": "Chicago"
}
}, {
type: 'Feature',
"geometry": { "type": "Point", "coordinates": [-74.00, 40.71]},
"properties": {
"image": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/NYC_Top_of_the_Rock_Pano.jpg/640px-NYC_Top_of_the_Rock_Pano.jpg",
"url": "https://en.wikipedia.org/wiki/New_York_City",
"city": "New York City"
}
}];
// Add custom popups to each using our custom feature properties
map.featureLayer.on('layeradd', function(e) {
var marker = e.layer,
feature = marker.feature;
// Create custom popup content
var popupContent = '<a target="_blank" class="popup" href="' + feature.properties.url + '">' +
'<img src="' + feature.properties.image + '">' +
' <h2>' + feature.properties.city + '</h2>' +
'</a>';
// http://leafletjs.com/reference.html#popup
marker.bindPopup(popupContent,{
closeButton: false,
minWidth: 320
});
});
// Add features to the map
map.featureLayer.setGeoJSON(geoJson);
map.setView([45.908, -78.525], 4);
var-map=L.mapbox.map('map','examples.map-9ijuk24y');
var geoJson=[{
键入:“功能”,
“几何学”:{“类型”:“点”,“坐标”:[-77.03,38.90]},
“财产”:{
“图像”:https://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Cherry_Blossoms_and_Washington_Monument.jpg/320px-Cherry_Blossoms_and_Washington_Monument.jpg",
“url”:”https://en.wikipedia.org/wiki/Washington“华盛顿大学”,
“标记符号”:“星形”,
“城市”:“华盛顿特区”
}
}, {
键入:“功能”,
“几何学”:{“类型”:“点”,“坐标”:[-87.63,41.88]},
“财产”:{
“图像”:https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Chicago_sunrise_1.jpg/640px-Chicago_sunrise_1.jpg",
“url”:”https://en.wikipedia.org/wiki/Chicago",
“城市”:“芝加哥”
}
}, {
键入:“功能”,
“几何学”:{“类型”:“点”,“坐标”:[-74.00,40.71]},
“财产”:{
“图像”:https://upload.wikimedia.org/wikipedia/commons/thumb/3/39/NYC_Top_of_the_Rock_Pano.jpg/640px-NYC_Top_of_the_Rock_Pano.jpg",
“url”:”https://en.wikipedia.org/wiki/New_York_City",
“城市”:“纽约市”
}
}];
//使用我们的自定义功能属性向每个窗口添加自定义弹出窗口
map.featureLayer.on('layeradd',函数(e){
var标记=e.layer,
feature=marker.feature;
//创建自定义弹出内容
var popupContent='';
// http://leafletjs.com/reference.html#popup
marker.bindPopup(popupContent{
closeButton:false,
最小宽度:320
});
});
//向地图添加要素
map.featureLayer.setGeoJSON(geoJson);
地图设置视图([45.908,-78.525],4);
更新:一个更简单的方法
var featureLayer = L.mapbox.featureLayer({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
title: location.title,
image: location.image,
},
geometry: {
type: 'Point',
coordinates: [0, 0]
}
}]
})
.loadURL('http://localhost:3000/locations.json')
.addTo(map);
featureLayer.eachLayer(function(layer) {
// here you call `bindPopup` with a string of HTML you create - the feature
// properties declared above are available under `layer.feature.properties`
var content = '<h1>size: ' + location.title + '<\/h1>' +
'<img src="' + location.image + '">';
layer.bindPopup(content);
});
var featureLayer=L.mapbox.featureLayer({
类型:“FeatureCollection”,
特点:[{
键入:“功能”,
特性:{
标题:location.title,
image:location.image,
},
几何图形:{
键入:“点”,
坐标:[0,0]
}
}]
})
.loadURL('http://localhost:3000/locations.json')
.addTo(地图);
featureLayer.eachLayer(功能(层){
//在这里,您使用创建的HTML字符串调用'bindpoppopup',这是一个特性
//上面声明的属性在“layer.feature.properties”下可用`
变量内容='size:'+location.title+'+
'';
层绑定弹出窗口(内容);
});
对我来说正确的代码是。。。我必须在AJAX调用中包装所有我想要的内容
<script type="text/javascript">
function load(geojson) {
// Fetch just the contents of a .geojson file from ... by passing
// `......` to the Accept header
// As with any other AJAX request, this technique is subject to the Same Origin Policy:
// http://en.wikipedia.org/wiki/Same_origin_policy the server delivering the request should support CORS.
$.ajax({
headers: {
'Accept': 'localhost'
},
dataType: 'json',
url: '/locations.json',
success: function (geojson) {
// setting URL for json call below.
// var url = '/locations.json';
// initializing map, dark theme, and view.
var map = L.mapbox.map('map', 'examples.map-y7l23tes')
.addControl(L.mapbox.geocoderControl('examples.map-y7l23tes'))
.setView([40.7178591652903, -73.985538482666], 12);
// Add custom popups to each using our custom feature properties
map.featureLayer.on('layeradd', function (e) {
var marker = e.layer,
feature = marker.feature;
// Create custom popup content
var popupContent = '<div class="popup">' + '<img src="' + feature.properties.image.image.url + '">' +
'<p style="font-size:94%">' + feature.properties.user_id + ' posted: ' +
'<p>' + feature.properties.title + '</p>' +
'<p><span class="timeago" title="' + feature.properties.created_at + '"></span></p>' +
'<% if can? :update, @location %><p><a href="/locations/' + feature.properties.name + '/edit">Edit</a> <% end %>' +
'<% if can? :destroy, @location %> | <%= link_to "Destroy", @location, method: :delete, data: { confirm: "Are you sure?" } %><% end %>';
// http://leafletjs.com/reference.html#popup
return marker.bindPopup(popupContent, {
closeButton: false,
minWidth: 320
});
});
// Iterate through each feature layer item, build a
// marker menu item and enable a click event that pans to + opens
// a marker that's associated to the marker item.
map.featureLayer.eachLayer(function (marker) {
var link = info.appendChild(document.createElement('a'));
link.className = 'item';
link.href = '#';
// Populate content from each markers object.
link.innerHTML = '<img src="' + marker.feature.properties.image.image.url + '" style="width:50px; height:auto;">' + marker.feature.properties.title +
'<br /><small>' + marker.feature.properties.user_id + '</small>';
link.onclick = function () {
if (/active/.test(this.className)) {
this.className = this.className.replace(/active/, '').replace(/\s\s*$/, '');
} else {
var siblings = info.getElementsByTagName('a');
for (var i = 0; i < siblings.length; i++) {
siblings[i].className = siblings[i].className
.replace(/active/, '').replace(/\s\s*$/, '');
};
this.className += ' active';
// When a menu item is clicked, animate the map to center
// its associated marker and open its popup.
map.panTo(marker.getLatLng());
marker.openPopup();
}
return false;
};
});
}
});
}
$(load);
</script>
函数加载(geojson){
//通过传递从…获取.geojson文件的内容
//“…”到接受标题
//与任何其他AJAX请求一样,此技术受同源策略的约束:
// http://en.wikipedia.org/wiki/Same_origin_policy 传递请求的服务器应该支持CORS。
$.ajax({
标题:{
“接受”:“本地主机”
},
数据类型:“json”,
url:“/locations.json”,
成功:函数(geojson){
//在下面设置json调用的URL。
//var url='/locations.json';
//初始化地图、暗主题和视图。
var map=L.mapbox.map('map','examples.map-y7l23tes')
.addControl(L.mapbox.geocoderControl('examples.map-y7l23tes'))
.setView([40.7178591652903,-73.985538482666],12);
//使用我们的自定义功能属性向每个窗口添加自定义弹出窗口
map.featureLayer.on('layeradd',函数(e){
var标记=e.layer,
feature=marker.feature;
//创建自定义弹出内容
var popupContent=“”+“”+
““+feature.properties.user_id+”发布:”+
“”+feature.properties.title+”
'+
“”+
“”+
' | ';
// http://leafletjs.com/reference.html#popup
返回marker.bindPopup(popupContent{
closeButton:false,
最小宽度:320
});
});
//迭代每个要素图层项,构建一个
//标记菜单项并启用平移到+打开的单击事件
//与标记项关联的标记。
map.featureLayer.eachLayer(功能(标记){
var link=info.appendChild(document.createElement('a'));
link.className='item';
link.href='#';
//从每个标记对象填充内容。
link.innerHTML=''+marker.feature.properties.title+
“
”+marker.feature.properties.user_id+”;
link.onclick=函数(){
if(/active/.test(this.className)){
this.className=this.className.replace(/active/,'').replace(/\s\s*$/,'');
}否则{
var sides=info.getElementsByTagName('a');
对于(变量i=0;i<1.length;i++){