名稱

ST_AsEncodedPolyline — 從 LineString 幾何圖形傳回編碼折線 (Encoded Polyline)。

概要

text ST_AsEncodedPolyline(geometry geom, integer precision=5);

描述

將幾何圖形傳回為編碼折線。此格式由 Google 地圖使用,精確度 (precision) 為 5;由 Open Source Routing Machine 使用,精確度為 5 和 6。

選用的 precision 指定在編碼折線中將保留多少位小數位數。編碼和解碼時數值應相同,否則坐標將不正確。

可用性:2.2.0

範例

基本

	SELECT ST_AsEncodedPolyline(GeomFromEWKT('SRID=4326;LINESTRING(-120.2 38.5,-120.95 40.7,-126.453 43.252)'));
	--result--
	|_p~iF~ps|U_ulLnnqC_mqNvxq`@
	

與地理線串 (geography linestring) 和地理分段 (geography segmentize) 結合使用,並放在 Google 地圖上

-- the SQL for Boston to San Francisco, segments every 100 KM
	SELECT ST_AsEncodedPolyline(
		ST_Segmentize(
			ST_GeogFromText('LINESTRING(-71.0519 42.4935,-122.4483 37.64)'),
				100000)::geometry) As encodedFlightPath;

javascript 看起來會像這樣,其中 $ 變數請替換為查詢結果

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<script type="text/javascript">
	 flightPath = new google.maps.Polyline({
			path:  google.maps.geometry.encoding.decodePath("$encodedFlightPath"),
			map: map,
			strokeColor: '#0000CC',
			strokeOpacity: 1.0,
			strokeWeight: 4
		});
</script>