当前位置: 首页 > 知识库问答 >
问题:

使用php和javascript从kml文件创建高程图

宿淳
2023-03-14

我试图使用谷歌高程服务来创建一个高程配置文件,就像这样:

https://google-developers.appspot.com/maps/documentation/javascript/examples/elevation-paths

下面是Javascript:

var elevator;
var map;
var chart;
var infowindow = new google.maps.InfoWindow();
var polyline;

// The following path marks a general path from Mt.
// Whitney, the highest point in the continental United
// States to Badwater, Death Vallet, the lowest point.
var whitney = new google.maps.LatLng(36.578581, -118.291994);
var lonepine = new google.maps.LatLng(36.606111, -118.062778);
var owenslake = new google.maps.LatLng(36.433269, -117.950916);
var beattyjunction = new google.maps.LatLng(36.588056, -116.943056);
var panamintsprings = new google.maps.LatLng(36.339722, -117.467778);
var badwater = new google.maps.LatLng(36.23998, -116.83171);

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

function initialize() {
  var mapOptions = {
    zoom: 8,
    center: lonepine,
    mapTypeId: 'terrain'
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

  // Create an ElevationService.
  elevator = new google.maps.ElevationService();

  // Draw the path, using the Visualization API and the Elevation service.
  drawPath();
}

function drawPath() {

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

  var path = [ whitney, lonepine, owenslake, panamintsprings, beattyjunction, badwater];

  // 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) {
    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);
    }

    // Display a polyline of the elevation path.
    var pathOptions = {
      path: elevationPath,
      strokeColor: '#0000CC',
      opacity: 0.4,
      map: map
    }
    polyline = new google.maps.Polyline(pathOptions);

    // 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, {
      width: 640,
      height: 200,
      legend: 'none',
      titleY: 'Elevation (m)'
    });
  }
}

我有一个KML文件。内容包括:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
    <name>test.kml</name>
    <Style id="inline10">
        <LineStyle>
            <color>ff0000ff</color>
            <width>2</width>
        </LineStyle>
        <PolyStyle>
            <fill>0</fill>
        </PolyStyle>
    </Style>
    <Style id="inline00">
        <LineStyle>
            <color>ff0000ff</color>
            <width>2</width>
        </LineStyle>
        <PolyStyle>
            <fill>0</fill>
        </PolyStyle>
    </Style>
    <StyleMap id="inline">
        <Pair>
            <key>normal</key>
            <styleUrl>#inline00</styleUrl>
        </Pair>
        <Pair>
            <key>highlight</key>
            <styleUrl>#inline10</styleUrl>
        </Pair>
    </StyleMap>
    <Placemark>
        <name>4.6m run</name>
        <styleUrl>#inline</styleUrl>
        <LineString>
            <tessellate>1</tessellate>
            <coordinates>
                -118.3701083851859,33.86613514741887,0 -118.3835891373556,33.86595745112309,0 -118.3837603618944,33.86620604860725,0 -118.3860197295395,33.865106678845,0 -118.3853630075581,33.86344608835453,0 -118.392210401906,33.86235962069966,0 -118.3930072160544,33.86482879105449,0 -118.3952920372758,33.86473719683653,0 -118.3955064755973,33.86544254118038,0 -118.3978346563853,33.87119781418026,0 -118.4006823491955,33.8746256060688,0 -118.395927369356,33.8729025811578,0 -118.3929679702478,33.87284041135064,0 -118.3912963149097,33.87280457074664,0 -118.3873145130551,33.86670833356382,0 -118.3845601044409,33.86794790820593,0 -118.3840431481755,33.86710880429249,0 -118.3704571100188,33.86705385780814,0 
            </coordinates>
        </LineString>
    </Placemark>
</Document>
</kml>

谷歌开发者博客上的例子从特定的lat和lon值创建了一条路径。我在kml文件的“坐标”下已经有了这些值。我想创建一个显示立面轮廓的图表。

此外,我能够使用“simplexml_load_file”解析kml文件以获得坐标字符串:

-118.3701083851859,33.86613514741887,0 -118.3835891373556,33.86595745112309,0 -118.3837603618944,33.86620604860725,0 -118.3860197295395,33.865106678845,0 -118.3853630075581,33.86344608835453,0 -118.392210401906,33.86235962069966,0 -118.3930072160544,33.86482879105449,0 -118.3952920372758,33.86473719683653,0 -118.3955064755973,33.86544254118038,0 -118.3978346563853,33.87119781418026,0 -118.4006823491955,33.8746256060688,0 -118.395927369356,33.8729025811578,0 -118.3929679702478,33.87284041135064,0 -118.3912963149097,33.87280457074664,0 -118.3873145130551,33.86670833356382,0 -118.3845601044409,33.86794790820593,0 -118.3840431481755,33.86710880429249,0 -118.3704571100188,33.86705385780814,0

我不知道我是否走对了路。

我想我需要换掉这个

var path = [ whitney, lonepine, owenslake, panamintsprings, beattyjunction, badwater];

对于lat,KML中的lon值。

我对谷歌api相当陌生。任何帮助都将不胜感激。

共有1个答案

哈雅珺
2023-03-14

使用geoxml3解析KML文件,找到其中的第一行字符串并绘制该路径的高程:

http://www.geocodezip.com/geoxml3_test/v3_geoxml3_kmltest_elevation_linkto.html

使用KML文件中的路线:

http://www.geocodezip.com/geoxml3_test/v3_geoxml3_kmltest_elevation_linkto.html?filename=ShaunWright_kml.xml

 类似资料:
  • 问题内容: 我想创建一个csv文件,但是当我运行代码时,它返回一个空白页,没有csv文件。我使用PHP5。我使用以下代码: 谢谢! 问题答案: 其空白,因为您正在写信。您应该写到using 代替,还应该发送标头信息以表明它是csv。 例

  • 文件已创建/打开并写入,但未下载。 有什么帮助吗?

  • 问题内容: 有人可以提供使用fgetcsv从CSV文件创建数组的代码吗? 我使用以下代码从一个简单的CSV文件创建一个数组,但是当我的一个字段包含多个逗号(例如地址)时,该方法将无法正常工作。 *此外,我的托管服务不支持str_getcsv。 上面的代码不适用于以下CSV文件示例。第一栏是姓名,第二栏是地址,第三栏是婚姻状况。 问题答案: 就像您在标题中所说的那样,fgetcsv是必经之路。它很容

  • 问题内容: 在创建用户个人资料系统时,我需要一些帮助。我希望它像Facebook或Myspace,在地址后仅包含用户名,没有问号,例如。我已经完成了所有注册,日志记录脚本等工作,但是如何使用上面的URL示例“ / username”转到配置文件? 问题答案: 您将需要创建一个mod重写,它采用第一个目录并将其作为$ _GET参数传递。 尝试这个: 那应该将’/’之后的内容重写为index.php?

  • 创建和使用钱包文件 为了离线脱机交易,你需要有你的钱包文件或与私密钱包/账户相关的公共和私人密钥。 web3j能够为你生成一个新的安全的以太坊钱包文件Ethereum wallet file,或者与也可以通过私钥来和现有的钱包文件一起工作。 创建新的钱包文件: String fileName = WalletUtils.generateNewWalletFile( "your pa

  • 我正在使用KML显示地图上的特定区域,你可以点击它并获得一些信息。应用程序必须脱机工作,所以我在本地使用KML文件。 我现在的问题是,当我的覆盖显示KML区域启用时,应用程序的整体速度降低了,并且在某个缩放级别之后,多边形变得太大而无法渲染。 我想获得一些使我的KML实现不那么重资源的一般提示,以及其他有关使用KML的OSMBonusPack的有用信息。 (示例:我注意到OSMDroid即使在我视