如何在Mapbox中使用位置数据加载本地存储项目并自动缩放到该位置?

时间:2019-01-23 18:41:02

标签: javascript local-storage mapbox geocoding

我在一页上有一个简单的文本提交字段。目的是让用户键入一个位置并点击“提交”,然后转到带有Mapbox地图的登录页面,使其居中或缩放到该位置。我尝试使用一个变量从本地存储传递地址-获取本地存储项并将其设置为地图中心-不走运。然后将其设置为新Mapbox地图的中心。我也可以使用Mapbox中的flyto方法来飞到地址位置。我不确定这是否会破坏地址解析器。这是我的代码:

第一页:

<html>   
<form target="_blank" id="form1">
<input name="address" type="text" id="address" placeholder="Location" style="border-radius: 5px;"/>
<input onclick="window.location.href = 'mymapboxpage';" type="Submit" value="GO" style="border-radius: 5px;"/>
</form>
</div>
<script type="text/javascript">
document.getElementById('form1')
.addEventListener('submit', function (event) {
event.preventDefault();
localStorage.setItem('address', document.getElementById('address').value);
});
</script>
</html>

带有Mapbox地图的着陆页:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Test</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; height:500px;}
#map { position:absolute; top:0; bottom:0; width:91%; height:500px;}
img {
height: 100px;
width: 120px;
}
.mapboxgl-ctrl-compass {
display: none !important;
}
</style>
</head>
<body>

<div id='map'></div>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.css' type='text/css' />
<script>
mapboxgl.accessToken = 'mycode12345';
var address = localStorage.getItem('address'); //local storage item from previous page
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
center: address, //How would I pass it to the map center?
zoom: 3
});

map.on('load', function() {
map.addSource("Properties", {
type: "geojson",
data: "mydataurl",
cluster: true,
clusterMaxZoom: 14, 
clusterRadius: 50
});

map.addLayer({
id: "clusters",
type: "circle",
source: "Properties",
filter: ["has", "point_count"],
paint: {
"circle-color": [
"step",
["get", "point_count"],
"#ffffff",
100,
"#ffffff",
750,
"#ffffff"
],
"circle-radius": [
"step",
["get", "point_count"],
20,
100,
30,
750,
40
]
}
});

map.addLayer({
id: "cluster-count",
type: "symbol",
source: "Properties",
filter: ["has", "point_count"],
layout: {
"text-field": "{point_count_abbreviated}",
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"],
"text-size": 12
}
});

map.addLayer({
id: "unclustered-point",
type: "circle",
source: "Properties",
filter: ["!", ["has", "point_count"]],
paint: {
"circle-color": "#ffffff",
"circle-radius": 8,
"circle-stroke-width": 1,
"circle-stroke-color": "#fff"
}
});

map.on('click', function(e) {
var features = map.queryRenderedFeatures(e.point, {
layers: ['unclustered-point'] // replace this with the name of the layer
});

if (!features.length) {
return;
}

var feature = features[0];

var popup = new mapboxgl.Popup({ offset: [0, -15] })
.setLngLat(feature.geometry.coordinates)
.setHTML('myhtml')
.setLngLat(feature.geometry.coordinates)
.addTo(map);
});
});

map.on('mouseenter', 'clusters', function () {
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseleave', 'clusters', function () {
map.getCanvas().style.cursor = '';
});
map.addControl(new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
placeholder: "Location",
animate: false,
zoom: 7.2
}), 'top-left');
map.addControl(new mapboxgl.NavigationControl(), 'bottom-right');
</script>

</body>
</html>

有什么想法吗?

0 个答案:

没有答案