forked from vicchi/maps-api-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathovi-geocoder.js
169 lines (138 loc) · 4.1 KB
/
ovi-geocoder.js
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
var map;
var geocoder;
var bubbleContainer;
function initialize() {
var initial_address = "Schönhauser Allee 180, Berlin, Germany";
geocoder = new ovi.mapsapi.search.Manager();
geocoder.addObserver("state", function(manager, key, value) {
if (value == "finished" || value == "failed") {
geocode_callback (manager.locations, value);
}
});
bubbleContainer = new ovi.mapsapi.map.component.InfoBubbles();
map = new ovi.mapsapi.map.Display(
document.getElementById("map"),
{
'zoomLevel': 8,
'center': [52.530390, 13.385190],
components: [
new ovi.mapsapi.map.component.Behavior(),
new ovi.mapsapi.map.component.ZoomBar(),
bubbleContainer
]
});
geocode(initial_address);
}
function geocode(address) {
geocoder.geocode(address);
}
function geocode_callback(response, status) {
if (status == "failed") {
var error_cause = ovi_geocoder.getErrorCause();
var error_status = "";
if (error_cause.type) {
error_status = error_cause.type;
if (error_cause.subtype) {
error_status += ", " + error_cause.subtype;
}
if (error_cause.message) {
error_status += ", " + error_cause.message;
}
}
else {
error_status = "Geocoding failure";
}
error_callback(error_status);
}
else if (status == "finished") {
var return_location = {};
var street_components = [];
var locality_components = [];
var region_components = [];
// Mapping ovi.mapsapi.search.Address to return_location ...
//
// return_location.street = Address.houseNumber + Address.street
// return_location.locality = Address.district + Address.city
// return_location.postcode = Address.postalCode
// return_location.region = Address.county + Address.state
// return_location.country = Address.country
return_location.street = '';
return_location.locality = '';
return_location.postcode = '';
return_location.region = '';
return_location.country = '';
if (response.length > 0) {
var address = response[0].address;
var coords = response[0].displayPosition;
if (address.street) {
street_components.push(address.street);
}
if (address.houseNumber) {
street_components.unshift(address.houseNumber);
}
if (address.city) {
locality_components.push(address.city);
}
if (address.district) {
locality_components.unshift(address.district);
}
if (address.postalCode) {
return_location.postcode = address.postalCode;
}
if (address.state) {
region_components.unshift(address.state);
}
if (address.county) {
region_components.push(address.county);
}
if (address.country) {
return_location.country = address.country;
}
if (return_location.street === '' && street_components.length > 0) {
return_location.street = street_components.join(' ');
}
if (return_location.locality === '' && locality_components.length > 0) {
return_location.locality = locality_components.join(', ');
}
if (return_location.region === '' && region_components.length > 0) {
return_location.region = region_components.join(', ');
}
return_location.point = coords;
success_callback(return_location);
}
}
}
function error_callback(status) {
geocode_status = document.getElementById("geocode_status");
geocode_status.innerHTML = status;
}
function success_callback (location) {
var infoBubble;
var components = [];
geocode_status = document.getElementById("geocode_status");
geocode_status.innerHTML = "geocoding succeeded";
map.setCenter(location.point);
var marker = new ovi.mapsapi.map.StandardMarker(location.point);
map.objects.add(marker);
if (location.street) {
components.push(location.street);
}
if (location.locality) {
components.push(location.locality);
}
if (location.postcode) {
components.push(location.postcode);
}
if (location.region) {
components.push(location.region);
}
if (location.country) {
components.push(location.country);
}
var bubble = components.join(', ');
infoBubble = bubbleContainer.addBubble(bubble, location.point);
}
function do_geocode() {
var address = document.getElementById("address").value;
geocode(address);
}