2016-08-01 66 views
2

我正在開發一個項目,該項目從GPX文件繪製到谷歌地圖上,並使用屬性對其進行顏色編碼(我們稱之爲Score)。我已經做了一些編輯,現在有一個額外的標記Score用於此GPX文件中的每個座標。這是我修改的GPX文件的一個片段。使用谷歌地圖API繪製改變顏色的多段線

<trkpt lat="50.834048" lon="-0.127354"> <ele>31.0</ele> 
<Score>-0.64730385</Score> 
<time>2016-07-07T14:31:51Z</time> 
</trkpt> 

<trkpt lat="50.833833" lon="-0.127600"> <ele>31.0</ele> 
<Score>-0.647203</Score> 
<time>2016-07-07T14:32:09Z</time> 
</trkpt> 

我正在學習Google Maps的JavaScript API來完成繪圖部分。我的問題是,如何創建一個基於Score更改顏色的ployline?我明白,我可以用兩點畫出數以千計的迷你多段線,併爲它們中的每一個設置顏色。但它感覺很原始。有沒有更好的方法來做到這一點?另外,我可以編寫自己的函數來解析此GPX文件,但爲什麼Google地圖不支持GPX?

如果它有助於說明我的目的,這是我儘量做到: enter image description here

+0

你看了API文檔和線索什麼都沒有? –

+1

沒關係 - [this](https://groups.google.com/forum/#!topic/google-maps-js-api-v3/7I8SSGpcAds)建議你不能做你想做的事 –

+0

我知道我可以策劃許多小小的「折線」只有兩點。但我的問題是有沒有更好的方法。 – Chris

回答

1

一種選擇是做一個單獨的折線每個線段,併爲其分配基於的分數顏色該部分的第一點。

for (var i=0;i<(trkpts.length-1);i++) { 
    var coord1 = new google.maps.LatLng(parseFloat(trkpts[i].getAttribute("lat")), 
             parseFloat(trkpts[i].getAttribute("lon"))); 
    var coord2 = new google.maps.LatLng(parseFloat(trkpts[i+1].getAttribute("lat")), 
             parseFloat(trkpts[i+1].getAttribute("lon"))); 
    var score = parseFloat(nodeValue(trkpts[i].getElementsByTagName("Score")[0])); 
    var polyline = new google.maps.Polyline({ 
    map: map, 
    path: [coord1, coord2], 
    strokeColor: rgb(-1,1,score), 
    strokeWeight: 4, 
    strokeOpacity: 1.0 
    }); 
} 

proof of concept fiddle

picture of fiddle

代碼片段:

var map; 
 
var bounds = new google.maps.LatLngBounds(); 
 

 
function initialize() { 
 
    map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(34, 108), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var gpxStr = '<gpx><trk><trkseg><trkpt lat="50.834048" lon="-0.127354"> <ele>31.0</ele><Score>-0.64730385</Score><time>2016-07-07T14:31:51Z</time></trkpt><trkpt lat="50.833833" lon="-0.127600"> <ele>31.0</ele><Score>-0.647203</Score><time>2016-07-07T14:32:09Z</time></trkpt><trkpt lat="50.833715" lon="-0.127768"> <ele>31.0</ele><Score>0.647203</Score><time>2016-07-07T14:32:50Z</time></trkpt><trkpt lat="50.833171" lon="-0.128468"> <ele>31.0</ele><Score>0.99609375</Score><time>2016-07-07T14:32:50Z</time></trkpt><trkpt lat="50.832951" lon="-0.128771"> <ele>31.0</ele><Score>-0.5</Score><time>2016-07-07T14:32:50Z</time></trkpt></trkseg></trk></gpx>'; 
 

 
    var xml = parseXml(gpxStr); 
 
    var trkpts = xml.getElementsByTagName("trkpt"); 
 
    var bounds = new google.maps.LatLngBounds(); 
 
    for (var i = 0; i < (trkpts.length - 1); i++) { 
 
    var coord1 = new google.maps.LatLng(parseFloat(trkpts[i].getAttribute("lat")), 
 
     parseFloat(trkpts[i].getAttribute("lon"))); 
 
    bounds.extend(coord1); 
 
    var coord2 = new google.maps.LatLng(parseFloat(trkpts[i + 1].getAttribute("lat")), 
 
     parseFloat(trkpts[i + 1].getAttribute("lon"))); 
 
    bounds.extend(coord2); 
 

 
    var score = parseFloat(nodeValue(trkpts[i].getElementsByTagName("Score")[0])); 
 
    var polyline = new google.maps.Polyline({ 
 
     map: map, 
 
     path: [coord1, coord2], 
 
     strokeColor: rgb(-1, 1, score), 
 
     strokeWeight: 4, 
 
     strokeOpacity: 1.0 
 
    }) 
 

 
    } 
 
    map.fitBounds(bounds); 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize); 
 

 
function rgb(minimum, maximum, value) { 
 
    var ratio = 2 * (value - minimum)/(maximum - minimum); 
 
    b = Math.floor(Math.max(0, 255 * (1 - ratio))); 
 
    r = Math.floor(Math.max(0, 255 * (ratio - 1))); 
 
    g = 255 - b - r; 
 

 
    var hexStr = ("00" + r.toString(16)).slice(-2); 
 
    hexStr += ("00" + g.toString(16)).slice(-2); 
 
    hexStr += ("00" + b.toString(16)).slice(-2); 
 
    hexStr = "#" + hexStr; 
 
    return hexStr 
 
} 
 
function parseXml(str) { 
 
    if (window.ActiveXObject) { 
 
     var doc = new ActiveXObject('MicrosoftXMLDOM'); 
 
     doc.loadXML(str); 
 
     return doc; 
 
    } else if (window.DOMParser) { 
 
     return (new DOMParser()).parseFromString(str, 'text/xml'); 
 
    } 
 
    } 
 
    //nodeValue: Extract the text value of a DOM node, with leading and trailing whitespace trimmed 
 

 
function nodeValue(node, defVal) { 
 
    var retStr = ""; 
 
    if (!node) { 
 
    return (typeof defVal === 'undefined' || defVal === null) ? '' : defVal; 
 
    } 
 
    if (node.nodeType == 3 || node.nodeType == 4 || node.nodeType == 2) { 
 
    retStr += node.nodeValue; 
 
    } else if (node.nodeType == 1 || node.nodeType == 9 || node.nodeType == 11) { 
 
    for (var i = 0; i < node.childNodes.length; ++i) { 
 
     retStr += arguments.callee(node.childNodes[i]); 
 
    } 
 
    } 
 
    return retStr; 
 
};
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>

+0

這正是我需要的。此外,我不知道,沒有你的優秀答案,stackoverflow支持這種代碼片段樣式 – Chris

相關問題