Skip to content

Instantly share code, notes, and snippets.

@geografa
Last active Aug 29, 2015
Embed
What would you like to do?
custom-marker-from-geojson
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title></title>
<script src='//api.tiles.mapbox.com/mapbox.js/v1.6.1/mapbox.js'></script>
<link href='//api.tiles.mapbox.com/mapbox.js/v1.6.1/mapbox.css' rel='stylesheet' />
<!--[if lte IE 8]>
<link href='//api.tiles.mapbox.com/mapbox.js/v1.6.1/mapbox.ie.css' rel='stylesheet'>
<![endif]-->
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
.marker {}
</style>
</head>
<body>
<div id='map'></div>
<script type='text/javascript'>
var map = L.mapbox.map('map', 'grafa.h0599p73')
.setView([45.68882429809956, -121.84547066688538], 16);
var featureLayer = L.mapbox.featureLayer()
.loadURL('map.geojson');
// function to run once marker has loaded
featureLayer.on('ready', function(){
// navigate the GeoJSON to get to the coordinates
var geojson = featureLayer.getGeoJSON();
var myIcon = L.icon({
iconUrl: 'https://i.cloudup.com/E44IxhcVLk.thumb.png',
iconSize: [75,75]
});
for (i = geojson.features.length - 1; i >= 0; i--) {
// add the coords in the correct order
var coords = geojson["features"][i]["geometry"]["coordinates"],
lat = coords[1],
lng = coords[0];
L.marker([lat,lng], {icon: myIcon})
.addTo(map);
};
});
</script>
</body>
</html>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment