| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
function swGmapWidget(options){ |
|---|
| 24 |
|
|---|
| 25 |
this.lng = null; |
|---|
| 26 |
this.lat = null; |
|---|
| 27 |
this.address = null; |
|---|
| 28 |
this.map = null; |
|---|
| 29 |
this.geocoder = null; |
|---|
| 30 |
this.options = options; |
|---|
| 31 |
|
|---|
| 32 |
this.init(); |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
swGmapWidget.prototype = new Object(); |
|---|
| 36 |
|
|---|
| 37 |
swGmapWidget.prototype.init = function(options) { |
|---|
| 38 |
|
|---|
| 39 |
if(!GBrowserIsCompatible()) |
|---|
| 40 |
{ |
|---|
| 41 |
return; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
this.lng = jQuery("#" + this.options.lng); |
|---|
| 46 |
this.lat = jQuery("#" + this.options.lat); |
|---|
| 47 |
this.address = jQuery("#" + this.options.address); |
|---|
| 48 |
this.lookup = jQuery("#" + this.options.lookup); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
this.geocoder = new GClientGeocoder(); |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
this.map = new GMap2(jQuery("#" + this.options.map).get(0)); |
|---|
| 55 |
this.map.setCenter(new GLatLng(this.lat.val(), this.lng.val()), 13); |
|---|
| 56 |
this.map.setUIToDefault(); |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
this.map.swGmapWidget = this; |
|---|
| 60 |
this.geocoder.swGmapWidget = this; |
|---|
| 61 |
this.lookup.get(0).swGmapWidget = this; |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
GEvent.addListener(this.map, "move", function() { |
|---|
| 65 |
var center = this.getCenter(); |
|---|
| 66 |
this.swGmapWidget.lng.val(center.lng()); |
|---|
| 67 |
this.swGmapWidget.lat.val(center.lat()); |
|---|
| 68 |
}); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
GEvent.addListener(this.map, "click", function(overlay, latlng) { |
|---|
| 72 |
if (latlng != null) { |
|---|
| 73 |
swGmapWidget.activeWidget = this.swGmapWidget; |
|---|
| 74 |
|
|---|
| 75 |
this.swGmapWidget.geocoder.getLocations( |
|---|
| 76 |
latlng, |
|---|
| 77 |
swGmapWidget.reverseLookupCallback |
|---|
| 78 |
); |
|---|
| 79 |
} |
|---|
| 80 |
}); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
this.lookup.bind('click', function(){ |
|---|
| 84 |
swGmapWidget.activeWidget = this.swGmapWidget; |
|---|
| 85 |
|
|---|
| 86 |
this.swGmapWidget.geocoder.getLatLng( |
|---|
| 87 |
this.swGmapWidget.address.val(), |
|---|
| 88 |
swGmapWidget.lookupCallback |
|---|
| 89 |
); |
|---|
| 90 |
|
|---|
| 91 |
return false; |
|---|
| 92 |
}) |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
swGmapWidget.activeWidget = null; |
|---|
| 96 |
swGmapWidget.lookupCallback = function(point) |
|---|
| 97 |
{ |
|---|
| 98 |
|
|---|
| 99 |
var widget = swGmapWidget.activeWidget; |
|---|
| 100 |
swGmapWidget.activeWidget = null; |
|---|
| 101 |
|
|---|
| 102 |
if (!point) { |
|---|
| 103 |
alert("address not found"); |
|---|
| 104 |
return; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
widget.map.clearOverlays(); |
|---|
| 108 |
widget.map.setCenter(point, 15); |
|---|
| 109 |
var marker = new GMarker(point); |
|---|
| 110 |
widget.map.addOverlay(marker); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
swGmapWidget.reverseLookupCallback = function(response) |
|---|
| 114 |
{ |
|---|
| 115 |
|
|---|
| 116 |
var widget = swGmapWidget.activeWidget; |
|---|
| 117 |
swGmapWidget.activeWidget = null; |
|---|
| 118 |
|
|---|
| 119 |
widget.map.clearOverlays(); |
|---|
| 120 |
|
|---|
| 121 |
if (!response || response.Status.code != 200) { |
|---|
| 122 |
alert('no address found'); |
|---|
| 123 |
return; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
var place = response.Placemark[0]; |
|---|
| 128 |
var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]); |
|---|
| 129 |
var marker = new GMarker(point); |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
widget.map.setCenter(point, 15); |
|---|
| 133 |
widget.map.addOverlay(marker); |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
widget.address.val(place.address); |
|---|
| 137 |
widget.lat.val(place.Point.coordinates[1]); |
|---|
| 138 |
widget.lng.val(place.Point.coordinates[0]); |
|---|
| 139 |
} |
|---|