-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeocode.html
179 lines (166 loc) · 5.09 KB
/
geocode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<!--
Copyright (c) 2013 Jean-Marc VIGLINO,
released under the CeCILL license (http://www.cecill.info/).
Utilisation des services Geoportail
-->
<html>
<head>
<title>Geoportail - Service de geocodage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../style.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" />
<script type="text/javascript" src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script type="text/javascript" src="../apikey.js"></script>
<script type="text/javascript" src="../geoportailconfig.js"></script>
<link rel="stylesheet" href="../ol3/geoportail.css" />
<script type="text/javascript" src="../ol3/geoportail.js"></script>
<script type="text/javascript" src="GeoportailService.js"></script>
<style>
#autoc a,
#resp a
{ display:block;
padding:0 0.5em;
text-decoration:none;
cursor:pointer;
}
#autoc a:hover,
#resp a:hover
{ background-color:#369;
color:#fff;
}
#map {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: -1;
}
</style>
<script type="text/javascript">
var map;
var service;
function initMap()
{ // Nouvelle carte
var carte = new ol.layer.Geoportail("GEOGRAPHICALGRIDSYSTEMS.MAPS");
// Photo IGN
var ortho = new ol.layer.Geoportail("ORTHOIMAGERY.ORTHOPHOTOS", {visible:false});
// Les couches
var layers = [ortho,carte];
// La carte
var map = new ol.Map.Geoportail
({ target: 'map',
key: apiKey,
//renderer: ["canvas", "webgl", "dom"], /* ["webgl", "canvas", "dom"] */
view: new ol.View
({ zoom: 7,
center: [288074, 6247982]
}),
controls: ol.control.defaults().extend
([ // new ol.control.FullScreen(),
// new ControlNord(),
new ol.control.ScaleLine()
]),
interactions: ol.interaction.defaults().extend
([ // new ol.interaction.DragRotateAndZoom()
]),
layers: layers
});
// Service de geocodage
service = new GeoportailService(apiKey);
// Geocoage inverse
map.on ('click', function(e){
var lonlat = ol.proj.transform(e.coordinate, map.getView().getProjection(), 'EPSG:4326');
console.log(e, lonlat)
service.reverseGeocode(lonlat[0],lonlat[1], function(resp){
console.log(arguments)
var a = resp[0];
if (a) {
$('.adress').html(
a.adresse.num+' '+a.adresse.rue+'<br/>'
+ a.adresse.cpost +' '+a.place.commune
)
} else {
$('.adress').html('-');
}
});
});
// Geocodage direct
$("#query")
// Autocompletion
.keyup(function(e)
{ var t = $(this).val();
if (t.length>3)
{ service.autocomplete(t, function(r)
{ if (r && r.length)
{ var auto = $("#autoc").html("").show();
for (var i=0; i<r.length; i++)
{ var a = $("<a>")
.click(function()
{ $("#query").val($(this).text());
$("#autoc").hide();
$("#resp").html("");
$("#query").change();
})
.text(r[i].fulltext)
.appendTo(auto);
}
}
else $("#autoc").hide("");
},
{ poi: $("#poi").is(":checked"),
adresse: $("#adresse").is(":checked"),
territoire:"ALL"
});
}
else $("#autoc").hide("");
})
// Geocodage
.change(function()
{ service.geocode($(this).val(), function(r)
{ var a, html = "";
if (r && r.length)
{ map.getView().setCenterAtLonlat([r[0].lon,r[0].lat]);
t = r.length +" réponses :";
for (var i=0; i<r.length; i++)
{ a = r[i];
html += "<a href='javascript:map.setCenterAtLonlat(["+a.lon+","+a.lat+"])'>"
+ (a.adresse.num?a.adresse.num:"") + " "
+ a.adresse.rue + " " + a.place.commune +" ("+a.place.departement+") "
+ a.place.nature
+"</a>";
}
}
else t = "<i>Pas de réponse...</i>";
$("#resp").html(html);
$("#autoc").hide();
},
{ max:20,
poi: $("#poi").is(":checked"),
adresse: $("#adresse").is(":checked"),
territoire:"ALL"
});
});
}
</script>
</head>
<body onload="initMap()">
<!-- DIV pour la carte -->
<div id="map"></div>
<div style="box-shadow: 5px 5px 5px rgba(0,0,0,.5); padding:0.5em; float: right; background: #fff;">
<p>
Position du clic:<br/>
<span class='adress' style="display: inline-block; width:90%; padding:.5em; border:1px solid #ccc;"></span>
</p>
<p style="margin:0;">Rechercher :</p>
<input id="adresse" type="checkbox" checked /><label for="adresse"> adresse</label>
<input id="poi" type="checkbox" /><label for="poi"> POI</label>
<br />
<input id="query" type="text" placeholder="Rechercher..." style="width:300px;" />
<div id="autoc" style="width:290px; clear:both; margin-left:10px; border:1px solid #369; padding:0 2px; display:none"></div>
<div id="resp" style="width:300px; clear:both;"></div>
</div>
</body>
</html>