- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 Leaflet API 构建 Web 应用程序。首先,我的用户使用 IP 进行地理定位,然后如果他接受,我会尝试使用 HTML5 地理定位更新他的位置(准确性更好)。
我的问题是 map 上的标记位置未更新,并且下面的代码失败且没有错误。我已经尝试了 leaflet documentation 中的数百种不同的语法和方法更新标记位置(即 setLatLng),但我没有成功。我想了解我的代码有什么问题。
<小时/>我的问题是这样解决的:
var lat = (e.latlng.lat);
var lng = (e.latlng.lng);
var newLatLng = new L.LatLng(lat, lng);
marker.setLatLng(newLatLng);
旧代码是:
//initial IP based geolocation
var lat = google.loader.ClientLocation.latitude;
var lng = google.loader.ClientLocation.longitude;
//place marker on the map
var marker = L.marker([lat,lng]).addTo(map);
//start HTML5 geolocation
map.locate({setView: true, maxZoom: 16});
function onLocationFound(e) {
var marker = L.marker([e.latlng.lat,e.latlng.lng]).update(marker);
alert ('New latitude is ' + e.latlng.lat)
}
map.on('locationfound', onLocationFound);
最佳答案
您问题中的代码有点令人困惑,当您只发布片段时很难说出问题是什么。
事实上,这段代码:
var lat = (e.latlng.lat);
var lng = (e.latlng.lng);
var newLatLng = new L.LatLng(lat, lng);
marker.setLatLng(newLatLng);
..应该在 onLocationFound()
内按预期工作。
你可以简化它:
marker.setLatLng(e.latlng);
但是,我想问题是范围问题,您的某些变量(例如标记)在 onLocationFound 内无法访问。
这里是如何实现它的示例:
function init(){
var map = L.map('map', {center: [51.505, -0.09], zoom: 13}),
marker = L.marker(map.getCenter()).addTo(map),
glcl = google.loader.ClientLocation,
onLocationfound = function(e){
marker.setLatLng(e.latlng);
map.setView(marker.getLatLng(),map.getZoom());
alert('Marker has been set to position :'+marker.getLatLng().toString());
};
L.tileLayer('http://{s}.tile.cloudmade.com/[yourCloudmadeKey]/997/256/{z}/{x}/{y}.png').addTo(map);
map.on('locationfound', onLocationfound);
if(glcl){//when google.loader.ClientLocation contains result
onLocationfound({latlng:[glcl.latitude,glcl.longitude]});
}else{alert('google.loader.ClientLocation fails');}
map.locate();
}
关于javascript - 使用传单 API 更新标记位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14173815/
我有一个带有 LeafletJS 的项目。 例如,我在 map 中有 2 个点 (A, B)。我将其显示为 2 个标记 我必须画一条从 A 到 B 的折线。 我移动了标记 A,我想将标记 A 的折线的
是否可以向自定义图标标记添加文本?我想避免在图像编辑器中编辑图标只是为了添加文本。 我已经像这样创建了我的自定义图标标记: var airfieldIcon = L.icon({ ic
我想要一个 Leaflet 图层控件,选中/取消选中时必须显示/隐藏主图层内的所有子图层。我还想独立检查/取消选中子层。我浏览了 Leaflet 文档和论坛,但找不到任何引用资料。我应该为此编写一个自
如何处理 Leaflet map 库中的重叠线? 我从服务器 sid 下载 geoJSON 并将其绘制到 map 上。如果有两个相同的条目,Leaflet 会绘制它们两次。这可以通过在服务器端查找完全
我在项目中使用 leafletjs。在 map 上我有几个多边形和标记。当其中一个被点击时,所有这些都有一个点击事件来显示一些信息。如果用户单击 map 的“空白”部分(而不是任何多边形或标记),我想
我正在尝试对下一个边界的外观做出预测。这是一个plunkr: https://plnkr.co/edit/sk6NRh51WZA2vpWP 这就是我让它工作的方式,但它不是很有效: const ori
我是 Leaflet 的新手,目前我正在努力学习教程。到目前为止,我设法创建了一个交互式 clorophet map ,就像示例中的 http://leafletjs.com/examples/cho
我很难弄清楚为什么我无法在 map 上绘制正确的国家/地区。我已经完成了所有代码,但我仍然不明白为什么不能正常工作。 如果您看到任何问题,请告诉我。我很欣赏。 这是数据集 Country
是否可以在使用 Stamen Toner-lite tiles 的 Leaflet map 上设置中间(2.5、3.5、4.5 等)缩放级别? ?这是我到目前为止计算缩放级别的代码: leafletm
早上、下午或晚上。 我有以下位置数据(从'Count of sampling points within a grid cell'调整) # Demo data set.seed(123) # lat
大家好,我正在开发一个具有 map 功能的网站。我的目标是,如果您单击标记,则使其拖动。 这是我的代码 else if (select1.value === "Arson"){ var note =
我正在开发一个应用程序,用户可以在其中上传KML文件,并使用Leaflet,toGeoJSON和Angular-Leaflet-Directive在屏幕上呈现路径。 我使用toGeoJSON.kml(
我需要在多边形要素中实现多色填充。填充将根据要素属性进行有条件的格式化。 假设我需要一个具有 3 色图案的多边形,如下所示: let fillPalette = ['orange', 'green',
我从 javascript 开始,并试图解决这个问题。 我希望当用户单击 map 时出现一个表单。当他们填写表单时,我想在该位置创建一个标记,并将该标记的弹出内容设置为表单中的值。添加表单完成后,我还
我已经在地理服务器中发布了一个功能,我可以通过传单及其 basemap 成功访问该功能。现在我需要在弹出窗口中获取feature onclick的属性信息。 我按原样使用了示例( https://gi
我正在使用带有一些标记的传单 map 。我还有一个侧边栏,可以在其中看到标记的名称,如果单击标记的名称, map 将缩放到标记的 lnglat。它已经可以工作,但我的问题是,当我刷新页面并在侧边栏中选
我正在使用 Leaflet 和 Mapbox,我想设置 map 的 View : 所有标记均可见 中心设置为特定点 使用 setView 和 fitbounds 单独完成每个点很容易,但我不知道如何同
是否有所有潜在 map 源的列表?在示例页面上,可以浏览四种类型的 map 。外面还有什么? http://tombatossals.github.io/angular-leaflet-directi
我正在使用 Leaflet 制作交互式 map 。我在 map 上有标记(基于其经度和纬度坐标),并且我希望某些标记具有与其他标记不同的颜色。例如,某些坐标我给它的分数是“10”,我希望它是粉红色的,
我可能忽略了如何在 map 上更改 LeftletJS 的光标。 http://leafletjs.com/reference.htm 我尝试设置map_div.style.cursor = 'cro
我是一名优秀的程序员,十分优秀!