我想在Openlayer2中添加多边形并计算距离

时间:2017-02-14 06:57:26

标签: openlayers

我有一个openlayer2的例子,我可以在其中绘制直线和圆形图,我可以计算距离。现在我想添加多边形并计算距离。因为我是新手,所以我收到了很多错误。原始代码在这里

  

https://bitbucket.org/geographika/openlayers/overview

有人可以帮忙吗?

<html style="width:100%; height:100%">
    <head>
    	<title>Geodesic Polygon Demo</title>
		<!-- by Seth Girvin, http://geographika.co.uk -->
        <script type="text/javascript" src="http://proj4js.org/lib/proj4js.js">
        </script>
        <script type="text/javascript" src="http://openlayers.org/api/OpenLayers.js">
        </script>
        <script type="text/javascript" src="GeodesicPolygon.js">
        </script>
        <script type="text/javascript" src="GeodesicTools.js">
        </script>
        <script type="text/javascript" src="http://extjs.cachefly.net/ext-2.2.1/adapter/ext/ext-base.js">
        </script>
        <script type="text/javascript" src="http://extjs.cachefly.net/ext-2.2.1/ext-all.js">
        </script>
        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/resources/css/ext-all.css" />
        <script type="text/javascript">
        </script>
    </head>
    <body style="width:100%; height:100%">
        <div style="width:100%; height:100%" id="map">
        </div>
    </body>
</html>
global $wpdb;

$booking_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_booking_order_item_id' AND meta_value = %d;", $item_id ) );

$wcBooking = new WC_Booking($booking_id);

$start = $wcBooking->get_start_date();
$end= $wcBooking->get_end_date();
$resource = $wcBooking->get_resource();
$option = $resource->post_title;

echo '<br/><small>From: '.$start.' </small>';
echo '<br/><small>To:  '.$end.' </small>';
echo '<br/><small>Option:  '.$option.' </small>';

2 个答案:

答案 0 :(得分:0)

我不完全确定您的意思是什么,但有一个问题是没有在heighthtml上设置body。这是OpenLayers的常见问题。由于它将地图放在div height: 100%;中,如果包含地图的元素不具有固定高度,则最终为零像素的100%。您可以使用固定的高度,也可以让所有父元素(html)的高度为100%

答案 1 :(得分:0)

用于计算多边形的距离/参数:

            var featureLength: number = 0;
        var polygonCoordinates = (<ol.geom.Polygon>olFeature.getGeometry()).getCoordinates();       //olFeature : ol.Feature object or you can use polygon object directly
        polygonCoordinates.forEach((coordinates) => {
            for (var i: number = 0; i < coordinates.length; i++) {
                console.log("polygon : " + coordinates[i] + " " + coordinates[(i + 1) % coordinates.length])
                var lineString = new ol.geom.LineString([coordinates[i], coordinates[(i + 1) % coordinates.length]]);
                featureLength += lineString.getLength();
            }