aboutsummaryrefslogtreecommitdiff
path: root/web/maps.js
blob: 2ae8cd8ef82c31d0eb3c54be2a2746feddaaaad4 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var map;
var mc;
var mapParams;
var mapBounds=null;
var cross=null;
var jcontrol=null;
var crossOnMap=false;

function mapInitAny() {
    map=new GMap2(document.getElementById("geomap"));
    map.setMapType(G_HYBRID_MAP);
    map.addControl(new GMapTypeControl());
    map.enableScrollWheelZoom();
}

function mapInitSimple(lat,lng,iconcolor) {
    mapInitAny();
    map.addControl(new GSmallZoomControl());
    map.setCenter(new GLatLng(lat,lng),15);

    var icon=new GIcon(G_DEFAULT_ICON);
    if(iconcolor && iconcolor!='red') icon.image="http://maps.google.com/mapfiles/marker_"+iconcolor+".png";

    map.addOverlay(new GMarker(new GLatLng(lat,lng),{
        'icon':icon,
        'clickable':false
    }));
}

function mapInit(params,autozoom,place) {
    mapParams=params;

    mapInitAny();
    map.addControl(new GLargeMapControl(),new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(-10,-60)));
    if(place) {
        jcontrol=new JuickControl();
        cross=new GScreenOverlay('http://static.juick.com/cross.png',
            new GScreenPoint(0.5, .5, 'fraction', 'fraction'),
            new GScreenPoint(9, 9),
            new GScreenSize(19, 19)
            );
    }
    map.setCenter(new GLatLng(30,0),2);

    mc=new MarkerClusterer(map,null,{
        gridSize:40,
        maxZoom:15
    });
  
    GEvent.addListener(map,"moveend",mapLoadMarkers);
    GEvent.addListener(map,"zoomend",mapLoadMarkers);

    if(autozoom==1 && navigator.geolocation) navigator.geolocation.getCurrentPosition(mapSetCenter,null,{
        timeout:5
    });
    else mapLoadMarkers(autozoom);
}

// call loadMarkers even if getCurrentPosition failed

function mapSetCenter(pos) {
    map.setCenter(new GLatLng(pos.coords.latitude,pos.coords.longitude),11);
    mapLoadMarkers();
}

function mapLoadMarkers(zoomOld) {
    var zoom=map.getZoom();
    if(zoom>14 && cross!=null && !crossOnMap) {
        map.addControl(jcontrol);
        map.addOverlay(cross);
        crossOnMap=true;
    }
    if(zoom<=14 && cross!=null && crossOnMap) {
        map.removeControl(jcontrol);
        map.removeOverlay(cross);
        crossOnMap=false;
    }
  
    var bounds=map.getBounds();
    if(mapBounds==null || !mapBounds.containsBounds(bounds) || zoomOld>0) {
        var span=bounds.toSpan();

        var swlat=bounds.getSouthWest().lat()-span.lat()/3;
        if(swlat<-90) swlat=-90;
        var swlng=bounds.getSouthWest().lng()-span.lng()/3;
        if(swlng<-180) swlng=-180;
    
        var nelat=bounds.getNorthEast().lat()+span.lat()/3;
        if(nelat>90) nelat=90;
        var nelng=bounds.getNorthEast().lng()+span.lng()/3;
        if(nelng>180) nelng=180;

        mapBounds=new GLatLngBounds(new GLatLng(swlat,swlng),new GLatLng(nelat,nelng));

        var q="/_mapxml?"+mapParams+"&south="+swlat+"&west="+swlng+"&north="+nelat+"&east="+nelng;
        GDownloadUrl(q,function(data) {
            var xmlmarkers=GXml.parse(data).documentElement.getElementsByTagName("marker");
            var markers=[];
            var mbounds=new GLatLngBounds();
            var icon;
            var iconcolor="null";
            for(var i=0; i<xmlmarkers.length; i++) {
                var latlng=new GLatLng(parseFloat(xmlmarkers[i].getAttribute("lat")),parseFloat(xmlmarkers[i].getAttribute("lon")));
                var iconcolornew=xmlmarkers[i].getAttribute("color");
                if(iconcolor!=iconcolornew) {
                    iconcolor=iconcolornew;
                    icon=new GIcon(G_DEFAULT_ICON);
                    if(iconcolor!="" && iconcolor!='red') icon.image="http://maps.google.com/mapfiles/marker_"+iconcolor+".png";
                }
                markers.push(
                    createMarker(
                        xmlmarkers[i].getAttribute("param"),
                        latlng,
                        xmlmarkers[i].getAttribute("title"),
                        xmlmarkers[i].getAttribute("href"),
                        icon
                        )
                    );
                if(zoomOld==-1) mbounds.extend(latlng);
            }
            mc.clearMarkers();
            mc.addMarkers(markers);
            if(zoomOld==-1) {
                var zoom=map.getBoundsZoomLevel(mbounds)-1;
                if(zoom>14) zoom=14;
                else if(zoom<1) zoom=1;
                map.setCenter(mbounds.getCenter(),zoom);
            }
        });
    }
}

function createMarker(id,latlng,title,href,icon) {
    var marker=new GMarker(latlng,{
        'icon':icon,
        'title':title
    });
    marker.param=id;
    if(href && href!="")
        GEvent.addListener(marker,"click",function(ll) {
            var txt='<a href="'+href+'">'+title+'</a>';
            map.openInfoWindowHtml(ll,txt);
        });
    return marker;
}

//

function JuickControl() {}
JuickControl.prototype = new GControl();
JuickControl.prototype.initialize = function(map) {
    var container = document.createElement("div");
  
    var bAddPlace = document.createElement("div");
    this.setButtonStyle_(bAddPlace);
    container.appendChild(bAddPlace);
    bAddPlace.appendChild(document.createTextNode("Add place"));
    GEvent.addDomListener(bAddPlace, "click", this.onAddPlaceClick);
    map.getContainer().appendChild(container);
    return container;
}

JuickControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 35));
}

JuickControl.prototype.setButtonStyle_ = function(button) {
    button.style.color = "#000000";
    button.style.backgroundColor = "white";
    button.style.font = "small Arial";
    button.style.border = "1px solid black";
    button.style.padding = "2px";
    button.style.marginBottom = "5px";
    button.style.textAlign = "center";
    button.style.width = "6em";
    button.style.cursor = "pointer";
}

var htmlAddPlace='<form><p style="width: 440px"><b>Name:</b><br/>\
<input type="text" name="description" maxlength="64" style="width: 400px"/><br/>\
Tags (separated by space, 10 max):\
<input type="text" name="tags" maxlength="255" style="width: 400px"/></p>\
<p style="width: 400px; text-align: right; margin-bottom: 0"><input type="button" value="   Add   " onclick="addPlace(this.form)"/></p>\
</form>';

var placeLatLng;

JuickControl.prototype.onAddPlaceClick = function() {
    placeLatLng=map.getCenter();
    map.removeOverlay(cross);
    map.openInfoWindowHtml(placeLatLng,htmlAddPlace,{
        onCloseFn:function() {
            map.panTo(placeLatLng);
            map.addOverlay(cross);
        }
    });
}

function addPlace(form) {
    var description=form.description.value;
    if(description=='') {
        alert('Enter place name.');
        return;
    }
    var tags=form.tags.value;
    map.closeInfoWindow();
    GDownloadUrl("/_mapxml",function(data) {
        var xmlmarkers=GXml.parse(data).documentElement.getElementsByTagName("marker");

        var icon=new GIcon(G_DEFAULT_ICON);
        icon.image="http://maps.google.com/mapfiles/marker_orange.png";
        var markers=[];
        markers.push(createMarker(
            xmlmarkers[0].getAttribute("param"),
            placeLatLng,
            description,
            xmlmarkers[0].getAttribute("href"),
            icon
            ));
        mc.addMarkers(markers);
    },'lat='+placeLatLng.lat()+'&lon='+placeLatLng.lng()+'&description='+escape(description)+'&tags='+escape(tags));
}