Last active
March 4, 2022 05:24
-
-
Save malwoodsantoro/8982e545752a3c8f3068dd01fe210f7c to your computer and use it in GitHub Desktop.
Place circles at geocoder result using turf.circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title>Set a point after Geocoder result</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.44.1/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' /> | |
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.1.1/mapbox-gl-geocoder.min.js'></script> | |
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.1.1/mapbox-gl-geocoder.css' type='text/css' /> | |
<style> | |
#geocoder-container > div { | |
min-width:50%; | |
margin-left:25%; | |
} | |
</style> | |
<div id='map'></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/streets-v9', | |
center: [-79.4512, 43.6568], | |
zoom: 13 | |
}); | |
var geocoder = new MapboxGeocoder({ | |
accessToken: mapboxgl.accessToken | |
}); | |
map.addControl(geocoder); | |
// After the map style has loaded on the page, add a source layer and default | |
// styling for a single point. | |
map.on('load', function() { | |
map.addSource('single-point', { | |
"type": "geojson", | |
"data": { | |
"type": "FeatureCollection", | |
"features": [] | |
} | |
}); | |
map.addLayer({ | |
"id": "point", | |
"source": "single-point", | |
"type": "circle", | |
"paint": { | |
"circle-radius": 10, | |
"circle-color": "#007cbf" | |
} | |
}); | |
// Listen for the `geocoder.input` event that is triggered when a user | |
// makes a selection and add a symbol that matches the result. | |
geocoder.on('result', function(ev) { | |
map.getSource('single-point').setData(ev.result.geometry); | |
console.log(ev.result.geometry) | |
var center = { | |
"type": "Feature", | |
"properties": { | |
"marker-color": "#0f0" | |
}, | |
"geometry": { | |
"type": "Point", | |
"coordinates": ev.result.geometry.coordinates | |
} | |
}; | |
var radius = 2; | |
var options = {steps: 100, units: 'kilometers', properties: {foo: 'bar'}}; | |
var circle = turf.circle(center, radius, options); | |
map.addLayer({ | |
"id": "circle-outline", | |
"type": "line", | |
"source": { | |
"type": "geojson", | |
"data": circle | |
}, | |
"paint": { | |
"line-color": "blue", | |
"line-opacity": 0.5, | |
"line-width": 10, | |
"line-offset": 5 | |
}, | |
"layout": { | |
} | |
}); | |
var radiusTwo = 4; | |
var circleTwo = turf.circle(center, radiusTwo, options); | |
map.addLayer({ | |
"id": "circle-outline-two", | |
"type": "line", | |
"source": { | |
"type": "geojson", | |
"data": circleTwo | |
}, | |
"paint": { | |
"line-color": "red", | |
"line-opacity": 0.5, | |
"line-width": 10, | |
"line-offset": 5 | |
}, | |
"layout": { | |
} | |
}); | |
var radiusThree = 6; | |
var circleThree = turf.circle(center, radiusThree, options); | |
map.addLayer({ | |
"id": "circle-outline-three", | |
"type": "line", | |
"source": { | |
"type": "geojson", | |
"data": circleThree | |
}, | |
"paint": { | |
"line-color": "green", | |
"line-opacity": 0.5, | |
"line-width": 10, | |
"line-offset": 5 | |
}, | |
"layout": { | |
} | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment