| 1 | == Select Location from Map == |
| 2 | |
| 3 | Example code from Sahana Agasti, which needs a little bit of porting from PHP to Python, some adjustment to the !JavaScript variable names & then merging into the [wiki:DeveloperGuidelinesGIS#GuidelinesforDeveloperswishingtomakeuseofMappingwithintheirModule mapping API] in {{{modules/s3gis.py}}}: |
| 4 | {{{ |
| 5 | var pointControl; |
| 6 | |
| 7 | // Add control to add new Points to the map. |
| 8 | pointControl = new OpenLayers.Control.DrawFeature(featuresLayer, OpenLayers.Handler.Point); |
| 9 | pointControl.featureAdded = shn_gis_map_add_geometry; |
| 10 | map.addControl(pointControl); |
| 11 | |
| 12 | function shn_gis_map_control_add_point(){ |
| 13 | shn_gis_map_control_deactivate_all(); |
| 14 | document.getElementById('gis_map_icon_addpoint').style.backgroundImage = "url(res/OpenLayers/theme/default/img/draw_point_on.png)"; |
| 15 | document.getElementById('gis_map_icon_descripion').innerHTML = '<?php echo _t('Mode: Add Point') ?>'; |
| 16 | pointControl.activate(); |
| 17 | } |
| 18 | |
| 19 | function shn_gis_map_add_geometry(feature){ |
| 20 | var fcopy = feature.clone(); |
| 21 | // need for later. |
| 22 | var fcopygeom = fcopy.geometry.clone(); |
| 23 | var lonlat = fcopy.geometry.getBounds().getCenterLonLat(); |
| 24 | var proj_current = map.getProjectionObject(); |
| 25 | lonlat.transform(proj_current, proj4326); |
| 26 | var lon_new = lonlat.lon; |
| 27 | var lat_new = lonlat.lat; |
| 28 | var wkt_new = fcopy.geometry.transform(proj_current, proj4326).toString(); |
| 29 | var type_new = featureTypeStr(fcopy); |
| 30 | |
| 31 | // Update form fields |
| 32 | var x_gps = document.getElementById("gps_x"); |
| 33 | var y_gps = document.getElementById("gps_y"); |
| 34 | if( x_gps != null && y_gps != null){ |
| 35 | x_gps.value = lon_new; |
| 36 | y_gps.value = lat_new; |
| 37 | } |
| 38 | |
| 39 | // store x,y coords in hidden variables named loc_x, loc_y |
| 40 | // must be set via calling page |
| 41 | var x_point = document.getElementsByName("loc_x"); |
| 42 | var y_point = document.getElementsByName("loc_y"); |
| 43 | if(x_point != null && y_point != null){ |
| 44 | x_point[0].value = lon_new; |
| 45 | y_point[0].value = lat_new; |
| 46 | } |
| 47 | // store type |
| 48 | var loc_type = document.getElementsByName("loc_type"); |
| 49 | if(loc_type != null){ |
| 50 | loc_type[0].value = type_new; |
| 51 | } |
| 52 | // store wkt value |
| 53 | var wkt_point = document.getElementsByName("loc_wkt"); |
| 54 | if(wkt_point != null){ |
| 55 | wkt_point[0].value = wkt_new; |
| 56 | } |
| 57 | |
| 58 | // Remove last plot from layer |
| 59 | featuresLayer.destroyFeatures(featuresLayer.features); |
| 60 | |
| 61 | // Add marker for visual confirmation |
| 62 | add_Feature(featuresLayer, 'newFeature', fcopygeom, '<?php echo $icon ?>'); |
| 63 | } |
| 64 | |
| 65 | // Add marker to map |
| 66 | function add_Feature(layer, feature_id, geom, iconURL){ |
| 67 | // Set icon dims |
| 68 | var icon_img = new Image(); |
| 69 | icon_img.src = iconURL; |
| 70 | var max_w = 25; |
| 71 | var max_h = 35; |
| 72 | var width = icon_img.width; |
| 73 | var height = icon_img.height; |
| 74 | if(width > max_w){ |
| 75 | height = ((max_w / width) * height); |
| 76 | width = max_w; |
| 77 | } |
| 78 | if(height > max_h){ |
| 79 | width = ((max_h / height) * width); |
| 80 | height = max_h; |
| 81 | } |
| 82 | // http://www.nabble.com/Markers-vs-Features--td16497389.html |
| 83 | var style_marker = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']); |
| 84 | //style_mark.pointRadius = 12; |
| 85 | style_marker.graphicWidth = width; |
| 86 | style_marker.graphicHeight = height; |
| 87 | style_marker.graphicXOffset = -(width / 2); |
| 88 | style_marker.graphicYOffset = -height; |
| 89 | style_marker.externalGraphic = iconURL; |
| 90 | style_marker.graphicOpacity = 1; |
| 91 | // Create Feature Vector + Props |
| 92 | var featureVec = new OpenLayers.Feature.Vector(geom, null, style_marker); |
| 93 | featureVec.fid = feature_id; |
| 94 | // Add Feature. |
| 95 | layer.addFeatures([featureVec]); |
| 96 | } |
| 97 | |
| 98 | // Deactivate all other controls |
| 99 | function shn_gis_map_control_deactivate_all(){ |
| 100 | // Turn off navigate |
| 101 | var nav = document.getElementById('gis_map_icon_select') |
| 102 | if(nav != null){ |
| 103 | nav.style.backgroundImage = "url(res/OpenLayers/theme/default/img/move_feature_off.png)"; |
| 104 | } |
| 105 | // Turn off select |
| 106 | if(selectControl != null){ |
| 107 | selectControl.unselectAll(); |
| 108 | selectControl.deactivate(); |
| 109 | document.getElementById('gis_map_icon_select').style.backgroundImage = "url(res/OpenLayers/theme/default/img/move_feature_off.png)"; |
| 110 | } |
| 111 | // Turn off drag |
| 112 | if(dragControl != null){ |
| 113 | dragControl.deactivate(); |
| 114 | document.getElementById('gis_map_icon_drag').style.backgroundImage = "url(res/OpenLayers/theme/default/img/pan_off.png)"; |
| 115 | } |
| 116 | // Turn off modify |
| 117 | //if(modifyControl != null){ |
| 118 | //modifyControl.deactivate(); |
| 119 | //} |
| 120 | // Drop features/popups in progress from a create feature. |
| 121 | if(currentFeature != null && ((pointControl != null && pointControl.active) || (lineControl != null && lineControl.active) || (polygonControl != null && polygonControl.active))){ |
| 122 | if(currentFeature.popup != null){ |
| 123 | currentFeature.popup.hide(); |
| 124 | currentFeature.popup.destroy(currentFeature.popup); |
| 125 | } |
| 126 | featuresLayer.removeFeatures([currentFeature]); |
| 127 | currentFeature.destroy(); |
| 128 | currentFeature = null; |
| 129 | } |
| 130 | // Hide any popup showing and deactivate current feature. |
| 131 | if(currentFeature != null){ |
| 132 | if(currentFeature.popup != null){ |
| 133 | currentFeature.popup.hide(); |
| 134 | } |
| 135 | currentFeature = null; |
| 136 | } |
| 137 | // Turn off point add |
| 138 | if(pointControl != null){ |
| 139 | pointControl.deactivate(); |
| 140 | document.getElementById('gis_map_icon_addpoint').style.backgroundImage = "url(res/OpenLayers/theme/default/img/draw_point_off.png)"; |
| 141 | } |
| 142 | // Turn off line add |
| 143 | if(lineControl != null){ |
| 144 | lineControl.deactivate(); |
| 145 | document.getElementById('gis_map_icon_addline').style.backgroundImage = "url(res/OpenLayers/theme/default/img/draw_line_off.png)"; |
| 146 | } |
| 147 | // Turn off polygon add |
| 148 | if(polygonControl != null){ |
| 149 | polygonControl.deactivate(); |
| 150 | document.getElementById('gis_map_icon_addpolygon').style.backgroundImage = "url(res/OpenLayers/theme/default/img/draw_polygon_off.png)"; |
| 151 | } |
| 152 | } |
| 153 | }}} |
| 154 | |
| 155 | ---- |
| 156 | BluePrintGeographicInformationSystems |
| 157 | |
| 158 | [wiki:BluePrintsBeginners#GISMapping] |
| 159 | |
| 160 | [wiki:Taiwan] |