From 2140e652d1c5712609b229019e610a64e3f766a4 Mon Sep 17 00:00:00 2001 From: bastianonm Date: Wed, 15 Feb 2012 16:04:18 +0000 Subject: [PATCH] --- WP-GPX-Maps.js | 119 ++++++++++++++++++----------- readme.txt | 23 ++++-- wp-gpx-maps.php | 134 +++++++++++++++++++++++---------- wp-gpx-maps_Utils.php | 8 +- wp-gpx-maps_admin_settings.php | 98 ++++++++++++++++++------ 5 files changed, 269 insertions(+), 113 deletions(-) diff --git a/WP-GPX-Maps.js b/WP-GPX-Maps.js index 987c8c9..55c17ab 100644 --- a/WP-GPX-Maps.js +++ b/WP-GPX-Maps.js @@ -4,18 +4,6 @@ */ -var loc_en = -{ - "length" : "Length", - "altitude": "Altitude" -}; -var loc_it = -{ - "length" : "Lunghezza", - "altitude": "Altitudine" -}; - -var loc = loc_en; var t; var funqueue = []; var infowindow @@ -26,9 +14,9 @@ var wrapFunction = function(fn, context, params) { }; } -function wpgpxmaps(targhetId,mapType,mapData,graphData,waypoints,unit,color1,color2) +function wpgpxmaps(params) { - funqueue.push( wrapFunction(_wpgpxmaps, this, [targhetId,mapType,mapData,graphData,waypoints,unit,color1,color2])); + funqueue.push( wrapFunction(_wpgpxmaps, this, [params])); unqueue(); } @@ -46,11 +34,23 @@ function unqueue() } } -function _wpgpxmaps(targhetId,mapType,mapData,graphData,waypoints,unit,color1,color2) +function _wpgpxmaps(params) { - var el = document.getElementById("wpgpxmaps_" + targhetId); - var el_map = document.getElementById("map_" + targhetId); - var el_chart = document.getElementById("chart_" + targhetId); + + var targetId = params.targetId; + var mapType = params.mapType; + var mapData = params.mapData; + var graphData = params.graphData; + var waypoints = params.waypoints; + var unit = params.unit; + var unitspeed = params.unitspeed; + var color1 = params.color1; + var color2 = params.color2; + var color3 = params.color3; + + var el = document.getElementById("wpgpxmaps_" + targetId); + var el_map = document.getElementById("map_" + targetId); + var el_chart = document.getElementById("chart_" + targetId); switch (mapType) { @@ -77,7 +77,8 @@ function _wpgpxmaps(targhetId,mapType,mapData,graphData,waypoints,unit,color1,co new google.maps.Point(0,0), new google.maps.Point(16, 32) ); - for(var i in waypoints) + + for (i=0; i < waypoints.length; i++) { addWayPoint(map, image, shadow, waypoints[i][0], waypoints[i][1], waypoints[i][2], waypoints[i][3]); } @@ -88,12 +89,14 @@ function _wpgpxmaps(targhetId,mapType,mapData,graphData,waypoints,unit,color1,co { var points = []; var bounds = new google.maps.LatLngBounds(); - for(var i in mapData) + + for (i=0; i < mapData.length; i++) { var p = new google.maps.LatLng(mapData[i][0], mapData[i][1]); points.push(p); - bounds.extend(p); + bounds.extend(p); } + var poly = new google.maps.Polyline({ path: points, strokeColor: color1, @@ -129,9 +132,11 @@ function _wpgpxmaps(targhetId,mapType,mapData,graphData,waypoints,unit,color1,co var l1 = event.latLng.Qa; if (!(l1)) l1 = event.latLng.Oa; + var l2 = event.latLng.Ra; if (!(l2)) - l2 = event.latLng.Pa; + l2 = event.latLng.Pa; + var ci = getClosestIndex(mapData,l1,l2); var r = chart.setSelection([{'row': parseInt(ci) + 1}]); } @@ -148,23 +153,56 @@ function _wpgpxmaps(targhetId,mapType,mapData,graphData,waypoints,unit,color1,co if (unit=="1") { - var numberFormat1 = "#,##0.#mi"; - var numberFormat2 = "#,###ft"; + numberFormat1 = "#,##0.#mi"; + numberFormat2 = "#,###ft"; } + + var showSpeed = (graphData[0].length == 3); var data = new google.visualization.DataTable(); - data.addColumn('number', loc['length']); - data.addColumn('number', loc['altitude']); - data.addRows(graphData); - var chart = new google.visualization.AreaChart(el_chart); + data.addColumn('number', "Distance"); + data.addColumn('number', "Elevation"); + var options = { curveType: "function", strictFirstColumnType: true, - hAxis : {format : numberFormat1, title : loc['length']}, - vAxis : {format : numberFormat2, title : loc['altitude']}, + hAxis : {format : numberFormat1}, + vAxis : {format : numberFormat2}, legend : {position : 'none'}, - chartArea: {left:70,top:10,width:"100%",height:"75%"}, - colors:[color2] + chartArea: {left:50,top:10,width:"100%",height:"75%"}, + colors:[color2,color3], + tooltip: { showColorCode: true}, + fontSize:11 }; + + if (showSpeed) + { + var speedFormat=""; + + if (unitspeed == '2') // miles/h + { + speedFormat = "#,##0.#mi/h"; + } + else if (unitspeed == '1') // km/h + { + speedFormat = "#,##0.#km/h"; + } + else + { + speedFormat = "#,##0.#m/s"; + } + data.addColumn('number', "Speed"); + options.vAxes = { 0:{format : numberFormat2, targetAxisIndex : 0}, + 1:{format : speedFormat, targetAxisIndex : 1} + }; + options.series = { 0:{color: color2, visibleInLegend: true, targetAxisIndex : 0}, + 1:{color: color3, visibleInLegend: true, targetAxisIndex : 1} + }; + options.chartArea.width="85%"; + //alert(el_chart.clientWidth); + } + + data.addRows(graphData); + var chart = new google.visualization.AreaChart(el_chart); chart.draw(data, options); google.visualization.events.addListener(chart, 'onmouseover', function (e) { @@ -227,13 +265,13 @@ function getClosestIndex(points,lat,lon) { var dd=10000; var ii=0; - for(var i in points) + for (i=0; i < points.length; i++) { var d = dist(points[i][0], points[i][1], lat, lon); - if (dhttp://www.pedemontanadelgrappa.it/category/mappe/ +Try this plugin: http://www.pedemontanadelgrappa.it/category/mappe/ Thanks to: www.securcube.net, www.darwinner.it, www.pedemontanadelgrappa.it, @@ -58,13 +58,17 @@ The attributes are: 1. pointsoffset: Skip points closer than XX meters(default is 10) -1. uom: the unit of measure values are: 0, 1 (0 = meters, 1 = miles/feet) +1. uom: the unit of measure of distance/altitude are values are: 0, 1 (0 = meters, 1 = miles/feet) 1. mlinecolor: map line color (default is #3366cc) -1. glinecolor: graph line color (default is #3366cc) +1. glinecolor: altitude line color (default is #3366cc) -shortcode with all the attributes : [sgpx gpx=">relative path to your gpx<" width=100% mheight=300px gheight=200px mtype=SATELLITE waypoints=true] +1. showspeed: show speed inside the chart (default is FALSE) + +1. glinecolorspeed: speed line color (default is #ff0000) + +1. uomspeed: the unit of measure of speed are: 0, 1, 2 (0 = m/s, 1 = km/h, 2 = miles/h) = What happening if I've a very large gpx? = This plugin will print a small amout of points to to speedup javascript and pageload. @@ -79,6 +83,11 @@ Yes! 2. Admin area - Settings == Changelog == += 1.1.6 = +* improved charts +* improved admin area +* added speed support (where aviable) +* fixed mootools incompability = 1.1.5 = * implemented cache (the plugin is much faster, especially on slow servers or external gpx) * minor bug fixes @@ -124,6 +133,8 @@ Yes! * Initial release. == Upgrade Notice == += 1.1.6 = +* Added speed support. To enable this feature please check the plugin settings = 1.1.5 = = 1.1.4 = = 1.1.3 = diff --git a/wp-gpx-maps.php b/wp-gpx-maps.php index bde5efd..cdde635 100644 --- a/wp-gpx-maps.php +++ b/wp-gpx-maps.php @@ -3,7 +3,7 @@ Plugin Name: WP-GPX-Maps Plugin URI: http://www.darwinner.it/ Description: Draws a gpx track with altitude graph -Version: 1.1.5 +Version: 1.1.6 Author: Bastianon Massimo Author URI: http://www.pedemontanadelgrappa.it/ License: GPL @@ -77,15 +77,18 @@ function handle_WP_GPX_Maps_Shortcodes($attr, $content='') $mt = findValue($attr, "mtype", "wpgpxmaps_map_type", "HYBRID"); $gh = findValue($attr, "gheight", "wpgpxmaps_graph_height", "200px"); $showW = findValue($attr, "waypoints", "wpgpxmaps_show_waypoint", false); + $showSpeed = findValue($attr, "showspeed", "wpgpxmaps_show_speed", false); $donotreducegpx = findValue($attr, "donotreducegpx", "wpgpxmaps_donotreducegpx", false); $pointsoffset = findValue($attr, "pointsoffset", "wpgpxmaps_pointsoffset", 10); $uom = findValue($attr, "uom", "wpgpxmaps_unit_of_measure", "0"); + $uomspeed = findValue($attr, "uomspeed", "wpgpxmaps_unit_of_measure_speed", "0"); $color_map = findValue($attr, "mlinecolor", "wpgpxmaps_map_line_color", "#3366cc"); $color_graph = findValue($attr, "glinecolor", "wpgpxmaps_graph_line_color", "#3366cc"); - + $color_graph_speed = findValue($attr, "glinecolorspeed", "wpgpxmaps_graph_line_color_speed", "#ff0000"); + $r = rand(1,5000000); - $cacheFileName = md5($gpx.$w.$mh.$mt.$gh.$showW.$donotreducegpx.$pointsoffset); + $cacheFileName = md5($gpx.$w.$mh.$mt.$gh.$showW.$donotreducegpx.$pointsoffset,$showSpeed); $gpxcache = gpxCacheFolderPath(); if(!(file_exists($gpxcache) && is_dir($gpxcache))) @@ -110,10 +113,10 @@ function handle_WP_GPX_Maps_Shortcodes($attr, $content='') $waypoints= ''; } } - - if ($points_maps == '') - { + if ($points_maps == '' || true) + { + $sitePath = sitePath(); $gpx = trim($gpx); @@ -127,24 +130,62 @@ function handle_WP_GPX_Maps_Shortcodes($attr, $content='') { $gpx = downloadRemoteFile($gpx); } + + if ($gpx == '') + { + return "No gpx found"; + } $points = getPoints( $gpx, $pointsoffset, $donotreducegpx); $points_maps = ''; $points_graph = ''; $waypoints = ''; + + if ($showSpeed == true) + { + + foreach ($points as $p) { + $points_maps .= '['.(float)$p[0].','.(float)$p[1].'],'; + + $_speed = $p[4]; // dafault m/s + + if ($uomspeed == '2') // miles/h + { + $_speed *= 2.2369362920544025; + } + else if ($uomspeed == '1') // km/h + { + $_speed *= 3.6; + } + + if ($uom == '1') + { + // Miles and feet + $points_graph .= '['.((float)$p[3]*0.000621371192).','.((float)$p[2]*3.2808399).','.$_speed.'],'; + } + else + { + $points_graph .= '['.(float)$p[3].','.(float)$p[2].','.$_speed.'],'; + } + } + } + else + { + + foreach ($points as $p) { + $points_maps .= '['.(float)$p[0].','.(float)$p[1].'],'; + + if ($uom == '1') + { + // Miles and feet + $points_graph .= '['.((float)$p[3]*0.000621371192).','.((float)$p[2]*3.2808399).'],'; + } + else + { + $points_graph .= '['.(float)$p[3].','.(float)$p[2].'],'; + } + } - foreach ($points as $p) { - $points_maps .= '['.(float)$p[0].','.(float)$p[1].'],'; - - if ($uom == '1') - { - // Miles and feet - $points_graph .= '['.((float)$p[3]*0.000621371192).','.((float)$p[2]*3.2808399).'],'; - } - else - { - $points_graph .= '['.(float)$p[3].','.(float)$p[2].'],'; - } } if ($showW == true) @@ -180,10 +221,17 @@ function handle_WP_GPX_Maps_Shortcodes($attr, $content='')
'; return $output; @@ -191,20 +239,25 @@ function handle_WP_GPX_Maps_Shortcodes($attr, $content='') function downloadRemoteFile($remoteFile) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $remoteFile); - curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); - curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); - $resp = curl_exec($ch); - curl_close($ch); - $tmpfname = tempnam ( '/tmp', 'gpx' ); - - $fp = fopen($tmpfname, "w"); - fwrite($fp, $resp); - fclose($fp); - - return $tmpfname; + try + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $remoteFile); + curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); + curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + $resp = curl_exec($ch); + curl_close($ch); + $tmpfname = tempnam ( '/tmp', 'gpx' ); + + $fp = fopen($tmpfname, "w"); + fwrite($fp, $resp); + fclose($fp); + + return $tmpfname; + } catch (Exception $e) { + return ''; + } } function unescape($value) @@ -220,10 +273,13 @@ function WP_GPX_Maps_install() { add_option("wpgpxmaps_height", '450px', '', 'yes'); add_option('wpgpxmaps_map_type','HYBRID','','yes'); add_option('wpgpxmaps_show_waypoint','','','yes'); + add_option('wpgpxmaps_show_speed','','','yes'); add_option('wpgpxmaps_pointsoffset','10','','yes'); add_option('wpgpxmaps_donotreducegpx','true','','yes'); - add_option("wpgpxmaps_unit_of_measure", 'mt', '', 'yes'); + add_option("wpgpxmaps_unit_of_measure", '0', '', 'yes'); + add_option("wpgpxmaps_unit_of_measure_speed", '0', '', 'yes'); add_option("wpgpxmaps_graph_line_color", '#3366cc', '', 'yes'); + add_option("wpgpxmaps_graph_line_color_speed", '#ff0000', '', 'yes'); add_option("wpgpxmaps_map_line_color", '#3366cc', '', 'yes'); } @@ -233,12 +289,14 @@ function WP_GPX_Maps_remove() { delete_option('wpgpxmaps_height'); delete_option('wpgpxmaps_map_type'); delete_option('wpgpxmaps_show_waypoint'); + delete_option('wpgpxmaps_show_speed'); delete_option('wpgpxmaps_pointsoffset'); delete_option('wpgpxmaps_donotreducegpx'); delete_option('wpgpxmaps_unit_of_measure'); + delete_option('wpgpxmaps_unit_of_measure_speed'); delete_option('wpgpxmaps_graph_line_color'); delete_option('wpgpxmaps_map_line_color'); - + delete_option('wpgpxmaps_graph_line_color_speed'); } ?> diff --git a/wp-gpx-maps_Utils.php b/wp-gpx-maps_Utils.php index 477662a..7ba9b6d 100644 --- a/wp-gpx-maps_Utils.php +++ b/wp-gpx-maps_Utils.php @@ -111,7 +111,7 @@ $points = array(); $gpx = simplexml_load_file($filePath); - if($gpx === FALSE) + if($gpx === FALSE) return; $gpx->registerXPathNamespace('10', 'http://www.topografix.com/GPX/1/0'); @@ -135,10 +135,12 @@ $lat = $trkpt['lat']; $lon = $trkpt['lon']; $ele = $trkpt->ele; + $speed = (float)$trkpt->speed; + if ($lastLat == 0 && $lastLon == 0) { //Base Case - array_push($points, array((float)$lat,(float)$lon,(float)round($ele,2),(float)round($dist,2))); + array_push($points, array((float)$lat,(float)$lon,(float)round($ele,2),(float)round($dist,2), $speed )); $lastLat=$lat; $lastLon=$lon; $lastEle=$ele; @@ -152,7 +154,7 @@ { //Bigger Offset -> write coordinate $lastOffset=0; - array_push($points, array((float)$lat,(float)$lon,(float)round($ele,1),(float)round($dist,1))); + array_push($points, array((float)$lat,(float)$lon,(float)round($ele,1),(float)round($dist,1), $speed )); } else { diff --git a/wp-gpx-maps_admin_settings.php b/wp-gpx-maps_admin_settings.php index 3d98dde..e153a4c 100644 --- a/wp-gpx-maps_admin_settings.php +++ b/wp-gpx-maps_admin_settings.php @@ -8,6 +8,7 @@ $donotreducegpx = get_option("wpgpxmaps_donotreducegpx"); $t = get_option('wpgpxmaps_map_type'); $uom = get_option('wpgpxmaps_unit_of_measure'); + $uomspeed = get_option('wpgpxmaps_unit_of_measure_speed'); if (!($t)) $t = 'HYBRID'; @@ -20,23 +21,45 @@ - + +

Map and Chart size

+ +
- + - + + + + + + + + +
Map and Graph size:Width: - Width: , - Map Height: , - Graph Height: +
Map Height: + +
Graph Height: + +
+ +

Maps

+ + - + + + + - +
Waypoints Support: onchange="this.value = (this.checked)" />Show Waypoints
Map line color: + +
Default Map Type: @@ -46,7 +69,11 @@ > TERRAIN: maps with physical features such as terrain and vegetation.
+ +

Chart

+ + - - - - - + - + + + + + + + + + +
Unit of measure: @@ -57,33 +84,58 @@
Map line color: - -
Graph line color:Elevation line color:
Advanced options:Show speed: -
- Do not edit if you don't know what you are doing!
- Skip points closer than meters. - onchange="this.value = (this.checked)" />Do not reduce gpx. + onchange="this.value = (this.checked)" />Show Speed (where aviable)
Speed line color: + +
Speed unit of measure: + +
+ +

Advanced options (Do not edit if you don't know what you are doing!)

+ + + + + + + + + + +
+ Skip points closer than meters. +
+ onchange="this.value = (this.checked)" />Do not reduce gpx. +
- + -

- +

+

\ No newline at end of file