Source

leaflet-search / demos / tests.html

The default branch has multiple heads

Full commit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="/leaflet/leaflet.css" />
<link rel="stylesheet" href="../leaflet-search.css" />
<link rel="stylesheet" href="../style.css" />
<style>
.maptest {
	border-radius:.125em;
	border:2px solid #1978cf;
	box-shadow: 0 0 8px #999;
	width:235px;
	height:240px;
}
.maptest_wrap {
	float:left;
	margin:10px;
}
h4 {
	margin:0
}
</style>
</head>

<body>
<h3><a href="../"><big></big> Leaflet.Control.Search</a></h3>
<h4>Testing area</h4>

<script src="/js/jquery-1.7.2.min.js"></script>
<script src="/leaflet/leaflet.js"></script>
<script src="../leaflet-search.js"></script>

<div class="maptest_wrap">
<h4>search in layer</h4>
<div id="map_simple" class="maptest"></div>
<script>

	var data = [
		{"loc":[41.575330,13.102411], "title":"aquamarine"},
		{"loc":[41.575730,13.002411], "title":"black"},
		{"loc":[41.807149,13.162994], "title":"blue"},
		{"loc":[41.507149,13.172994], "title":"chocolate"},
		{"loc":[41.847149,14.132994], "title":"coral"},
		{"loc":[41.219190,13.062145], "title":"cyan"},
		{"loc":[41.344190,13.242145], "title":"darkblue"},	
		{"loc":[41.679190,13.122145], "title":"darkred"},
		{"loc":[41.329190,13.192145], "title":"darkgray"},
		{"loc":[41.379290,13.122545], "title":"dodgerblue"},
		{"loc":[41.409190,13.362145], "title":"gray"},
		{"loc":[41.794008,12.583884], "title":"green"},	
		{"loc":[41.805008,12.982884], "title":"greenyellow"},
		{"loc":[41.536175,13.273590], "title":"red"},
		{"loc":[41.516175,13.373590], "title":"rosybrown"},
		{"loc":[41.506175,13.173590], "title":"royalblue"},
		{"loc":[41.836175,13.673590], "title":"salmon"},
		{"loc":[41.796175,13.570590], "title":"seagreen"},
		{"loc":[41.436175,13.573590], "title":"seashell"},
		{"loc":[41.336175,13.973590], "title":"silver"},
		{"loc":[41.236175,13.273590], "title":"skyblue"},
		{"loc":[41.546175,13.473590], "title":"yellow"},
		{"loc":[41.239190,13.032145], "title":"white"}
	];

	var map = new L.Map('map_simple', {zoom: 9, center: new L.latLng(data[0].loc) });	//set center from first location

	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	var markersLayer = new L.LayerGroup();	//layer contain searched elements
	map.addLayer(markersLayer);

	map.addControl( new L.Control.Search({layer: markersLayer}) );  //inizialize search control

	////////////populate map with markers from sample data
	for(i in data) {
		var title = data[i].title,	//value searched
			loc = data[i].loc,		//position found
			marker = new L.Marker(new L.latLng(loc), {title: title} );//se property searched
		marker.bindPopup('title: '+ title );
		markersLayer.addLayer(marker);
	}
</script>
</div>

<div class="maptest_wrap">
<h4>search in GeoJSON</h4>
<div id="map_vector" class="maptest"></div>
<script src="us-states.js"></script>
<script>

	var map = new L.Map('map_vector', {zoom: 5, center: new L.latLng([37.8, -96]) });

	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	var featuresLayer = new L.GeoJSON(us_states, {
			style: function(f) { return {color: f.properties.color }; }
		});

	map.addLayer(featuresLayer);

	map.addControl( new L.Control.Search({layer: featuresLayer, propertyName: 'name'}) );  //inizialize search control

</script>
</div>

<div class="maptest_wrap">
<h4>Ajax</h4>
<div id="map_ajax" class="maptest"></div>
<script>

	var map = new L.Map('map_ajax', {zoom: 9, center: new L.latLng([41.575730,13.002411]) });

	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	map.addControl( new L.Control.Search({url: 'search.php?q={s}', text:'Color...', markerLocation: true}) );
	//view source of search.php for more details

</script>
</div>

<div class="maptest_wrap">
<h4>JSONP</h4>
<div id="map_jsonp" class="maptest"></div>
<script>

	var map = new L.Map('map_jsonp', {zoom: 9, center: new L.latLng([41.575730,13.002411]) });

	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	map.addControl( new L.Control.Search({url: 'search.php?q={s}', jsonpParam:'callback', text:'Color...', markerLocation: true}) );
	//view source of search.php for more details

</script>
</div>

<div class="maptest_wrap">
<h4>callData AJAX(by jQuery)</h4>
<div id="map_ajaxjquery" class="maptest"></div>
<script>

	var map = new L.Map('map_ajaxjquery', {zoom: 9, center: new L.latLng([41.575730,13.002411]) });

	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	function searchByAjax(text, callResponse)//callback for 3rd party ajax requests
	{
		$.ajax({
			url: 'search.php',	//read comments in search.php for more information usage
			type: 'GET',
			data: {q: text},
			dataType: 'json',
			success: callResponse
		});
	}

	map.addControl( new L.Control.Search({callData: searchByAjax, text:'Color...', markerLocation: true}) );

</script>
</div>

<div class="maptest_wrap">
<h4>jsonpUrl, filterJSON, Cities</h4>
<div id="map_filterjson" class="maptest"></div>
<script>

	var map = new L.Map('map_filterjson', {zoom: 9, center: new L.latLng([41.575730,13.002411]) });
	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer
	
	var jsonpUrl = 'http://open.mapquestapi.com/nominatim/v1/search.php?q={s}'+
				   '&format=json&osm_type=N&limit=100&addressdetails=0',
		jsonpName = 'json_callback';

	function filterJSONCall(rawjson) {	//callback that remap fields name
		var json = {},
			key, loc, disp = [];
			
		for(var i in rawjson)
		{	
			disp = rawjson[i].display_name.split(',');	

			key = disp[0] +', '+ disp[1];
			
			loc = L.latLng( rawjson[i].lat, rawjson[i].lon );
			
			json[ key ]= loc;	//key,value format
		}
		
		return json;
	}
	
	var searchOpts = {
			url: jsonpUrl,
			jsonpParam: jsonpName,
			filterJSON: filterJSONCall,
			animateLocation: true,
			markerLocation: true,
			zoom:10,
			minLength: 2,
			autoType: false
		};
		
	map.addControl( new L.Control.Search(searchOpts) );

</script>
</div>

<div class="maptest_wrap">
<h4>callData ajax bulk, Cities(by jQuery)</h4>
<div id="map_ajaxbulk" class="maptest"></div>
<script>

	var map = new L.Map('map_ajaxbulk', {zoom: 9, center: new L.latLng([41.575730,13.002411]) });
	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	map.addControl( new L.Control.Search({ url: 'searcp.php?cities=1&q={s}', text:'City name...', markerLocation: true }) );

</script>
</div>


<div class="maptest_wrap">
<h4>search in layer, Custom Tip</h4>
<div id="map_customtip" class="maptest"></div>
<script>

	var map = new L.Map('map_customtip', {zoom: 9, center: new L.latLng(data[0].loc) });	//set center from first location

	map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'));	//base layer

	var markersLayer = new L.LayerGroup();	//layer contain searched elements
	map.addLayer(markersLayer);

	function customTip(text)
	{
		var tip = L.DomUtil.create('a', 'colortip');

		tip.href = "#"+text;
		tip.innerHTML = text;
		
		var subtip = L.DomUtil.create('em', 'subtip', tip);//colored square
		subtip.style.display = 'inline-block';
		subtip.style.float = 'right';
		subtip.style.width = '14px';
		subtip.style.height = '14px';
		subtip.style.backgroundColor = text;
		
		return tip;
	}

	map.addControl( new L.Control.Search({layer: markersLayer, callTip: customTip, autoType: false }) );  //inizialize search control

	////////////populate map with markers from sample data
	for(i in data) {
		var title = data[i].title,	//value searched
			loc = data[i].loc,		//position found
			marker = new L.Marker(new L.latLng(loc), {title: title} );//se property searched
		marker.bindPopup('title: '+ title );
		markersLayer.addLayer(marker);
	}
</script>
</div>

<div id="copy">powered by <a href="mailto:stefano.cudini@gmail.com">Stefano Cudini</a></div>
</body>
</html>