2014-09-06 170 views
1

我有一个包含带有线条的GeoJson文件的地图,显示一些路径。是否可以使用Google Maps API Elevation Service为GeoJson文件的每个要素线创建高程配置文件?我希望在单击其中一行时显示高程配置文件。从GeoJson功能创建高程配置文件

事情是这样的例子:http://www.trailforks.com/region/la-bouilladisse/

我的代码,到现在为止,看起来是这样的:

google.load("visualization", "1", {packages: ["columnchart"]}); 

function initialize() { 
var options = { 
    center: new google.maps.LatLng(44.701991, 22.624884), 
    zoom: 12, 
    mapTypeId: google.maps.MapTypeId.TERRAIN 
}; 

map = new google.maps.Map(document.getElementById("map"), options); 

trasee = new google.maps.Data() 
trasee.loadGeoJson('http://googledrive.com/host/0B55_4P6vMjhITEU4Ym9iVG8yZUU/trasee.geojson') 
trasee.setMap(map) 

styling = (function(feature) { 
    var clasificare = feature.getProperty('Tip_drum'); 
    var culoare; 
    if (clasificare == ('Poteca')) 
     (culoare = 'brown') 
    else if (clasificare == ('Drum forestier')) 
     (culoare = 'green') 
    else if (clasificare == ('Drum comunal (neasfaltat)')) 
     (culoare = 'brown') 
    else if (clasificare == ('Drum judetean (neasfaltat)')) 
     (culoare = 'brown') 
    else if (clasificare == ('Drum comunal (asfaltat)')) 
     (culoare = 'gray') 
    else if (clasificare == ('Drum judetean (asfaltat)')) 
     (culoare = 'gray') 
    else 
     (culoare = 'black') 
    return ({ 
     strokeColor: culoare, 
     strokeWeight: 3 
    }) 
}) 

trasee.setStyle(styling) 

elevator = new google.maps.ElevationService(); 
} 

我知道我不得不作出这样的要求: VAR pathRequest = { “路径':用于创建路径的latlng的源 '样本':256 }

所以基本上,我认为GeoJson必须添加到某处n pathRequest,但我不知道如何以及如何为GeoJson文件中的每个要素创建不同的高程图。

fiddle of existing code

好了,现在我试着在信息窗口的Tip_drum属性沿着显示海拔图表,当我点击数据。我添加了以下代码:

map.data.addListener('click', function (event) { 
    document.getElementById('info').innerHTML = event.feature.getProperty('Tip_drum') 
    var content = document.createElement('div') 
    var elevations = document.getElementById('elevation_chart') 
    var types = document.getElementById('info') 
    content.appendChild(elevations) 
    content.appendChild(types) 
    infowindow.setContent(content) 
    infowindow.setPosition(event.latLng) 
    infowindow.setMap(map) 
    if (event.feature.getGeometry().getType() === 'LineString') { 
     drawPath(event.feature.getGeometry().getArray()); 

一切工作正常,直到我手动关闭其中一个infowindows。之后,infowindows将不再出现。

+0

当然,这是可能。你的代码是什么样的? – geocodezip 2014-09-06 15:52:51

+0

当然,您可以在点击时添加线条的高程配置文件。有很多线条,因此您需要根据需要生成高程图(点击线条时)。您可以将点击侦听器添加到数据层,获取路径坐标并将其发送到提升服务。 – geocodezip 2014-09-07 13:07:50

回答

2
  1. 得到点击功能的路径(event.feature.getGeometry()的getArray())
  2. 它传递给标高服务(如在elevations service documentation的例子)
  3. 情节返回的数据在图表上(如海拔服务文档中的示例)
  4. 从Google提升服务示例中删除代码,该示例根据数据层的多段线创建蓝色折线。

(注意,有些上面没有与您现有的代码工作,我修改了它稍微相匹配的文档中的工作实例)

working fiddle

var elevator; 
var map; 
var chart; 
var polyline; 

// Load the Visualization API and the columnchart package. 
google.load('visualization', '1', { 
    packages: ['columnchart'] 
}); 

function initialize() { 
    var options = { 
     center: new google.maps.LatLng(44.701991, 22.624884), 
     zoom: 12, 
     mapTypeId: google.maps.MapTypeId.TERRAIN 
    }; 

    map = new google.maps.Map(document.getElementById("map"), options); 

    trasee = new google.maps.Data(); 
    map.data.loadGeoJson('http://googledrive.com/host/0B55_4P6vMjhITEU4Ym9iVG8yZUU/trasee.geojson'); 

    // trasee.setMap(map); 

    styling = (function (feature) { 
     var clasificare = feature.getProperty('Tip_drum'); 
     var culoare; 
     if (clasificare == ('Poteca')) 
     (culoare = 'brown'); 
     else if (clasificare == ('Drum forestier')) 
     (culoare = 'green'); 
     else if (clasificare == ('Drum comunal (neasfaltat)')) 
     (culoare = 'brown'); 
     else if (clasificare == ('Drum judetean (neasfaltat)')) 
     (culoare = 'brown'); 
     else if (clasificare == ('Drum comunal (asfaltat)')) 
     (culoare = 'gray'); 
     else if (clasificare == ('Drum judetean (asfaltat)')) 
     (culoare = 'gray'); 
     else(culoare = 'black'); 
     return ({ 
      strokeColor: culoare, 
      strokeWeight: 3 
     }); 
    }); 

    map.data.setStyle(styling); 

    // Set mouseover event for each feature. 
    map.data.addListener('click', function (event) { 
     document.getElementById('info').innerHTML = event.feature.getProperty('Tip_drum'); 
     if (event.feature.getGeometry().getType() === 'LineString') { 
      drawPath(event.feature.getGeometry().getArray()); 

      // calculate the directions once both origin and destination are set 
      // calculate(); 
     } 
    }); 

    // When the user hovers, tempt them to click by outlining the letters. 
    // Call revertStyle() to remove all overrides. This will use the style rules 
    // defined in the function passed to setStyle() 
    map.data.addListener('mouseover', function(event) { 
    map.data.revertStyle(); 
     map.data.overrideStyle(event.feature, {strokeWeight: 8, strokeColor: 'blue'}); 
    }); 

    map.data.addListener('mouseout', function(event) { 
    map.data.revertStyle(); 
    }); 
    elevator = new google.maps.ElevationService(); 
    // Draw the path, using the Visualization API and the Elevation service. 
    // drawPath(); 
} 

function drawPath(path) { 

    // Create a new chart in the elevation_chart DIV. 
    chart = new google.visualization.ColumnChart(document.getElementById('elevation_chart')); 

    // Create a PathElevationRequest object using this array. 
    // Ask for 256 samples along that path. 
    var pathRequest = { 
     'path': path, 
      'samples': 256 
    }; 

    // Initiate the path request. 
    elevator.getElevationAlongPath(pathRequest, plotElevation); 
} 

// Takes an array of ElevationResult objects, draws the path on the map 
// and plots the elevation profile on a Visualization API ColumnChart. 
function plotElevation(results, status) { 
    if (status != google.maps.ElevationStatus.OK) { 
     return; 
    } 
    var elevations = results; 

    // Extract the elevation samples from the returned results 
    // and store them in an array of LatLngs. 
    var elevationPath = []; 
    for (var i = 0; i < results.length; i++) { 
     elevationPath.push(elevations[i].location); 
    } 

    // Extract the data from which to populate the chart. 
    // Because the samples are equidistant, the 'Sample' 
    // column here does double duty as distance along the 
    // X axis. 
    var data = new google.visualization.DataTable(); 
    data.addColumn('string', 'Sample'); 
    data.addColumn('number', 'Elevation'); 
    for (var i = 0; i < results.length; i++) { 
     data.addRow(['', elevations[i].elevation]); 
    } 

    // Draw the chart using the data within its DIV. 
    document.getElementById('elevation_chart').style.display = 'block'; 
    chart.draw(data, { 
     height: 150, 
     legend: 'none', 
     titleY: 'Elevation (m)' 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
+0

哇,非常感谢,我仍然试图理解你的代码的一些部分(我是JS的初学者),但它很好用。有时在单击GeoJson特征后,蓝色路径将在GeoJSON上方绘制,并保留在那里,因此我将路径的strokeOpacity更改为0.0。 – 2014-09-08 08:23:29

+0

从海拔服务示例[jsfiddle](http://jsfiddle.net/j54gLjap/3/)删除代码以在代码中创建折线(更新上面的代码)。 – geocodezip 2014-09-08 09:35:19

+0

好吧,现在我尝试在infowindows中显示海拔图以及Tip_drum属性,当我单击数据时。一切工作正常,直到我手动关闭其中一个infowindows。之后,infowindows将不再出现。我在第一篇文章中添加了infowindow的代码。 – 2014-09-08 12:20:28