【问题标题】:How do I use the postal code overlay with fills and specific postal codes only?如何仅使用带有填充和特定邮政编码的邮政编码覆盖?
【发布时间】:2018-12-13 17:45:01
【问题描述】:

/**
 * Shows the postcode layer provided by Platform Data Extension REST API
 * https://developer.here.com/platform-extensions/documentation/platform-data/topics/introduction.html
 *
 * @param  {H.Map} map      A HERE Map instance within the application
 */
function showPostcodes(map, bubble){
  var service = platform.getPlatformDataService();

  style = new mapsjs.map.SpatialStyle({ strokeColor: 'red', fillColor: 'rgba(0, 85, 170, 0.4)' });
  
  //style.setStyle({strokeColor:'Red'});

  
  // create tile provider and layer that displays postcode boundaries
  var boundariesProvider = new mapsjs.service.extension.platformData.TileProvider(service,
  {
    layerId: 'PSTLCB_GEN', level: 12
  }, {
      resultType: mapsjs.service.extension.platformData.TileProvider.ResultType.POLYLINE,
      styleCallback: function(data) {return style}
  });
  var boundaries = new mapsjs.map.layer.TileLayer(boundariesProvider);
  map.addLayer(boundaries);

  // create tile provider and layer that displays postcode centroids
  /*
  var centroidsProvider = new mapsjs.service.extension.platformData.TileProvider(service,
  {
    layerId: 'PSTLCB_MP', level: 12
  }, {
      resultType: mapsjs.service.extension.platformData.TileProvider.ResultType.MARKER
  });
  var centroids = new mapsjs.map.layer.MarkerTileLayer(centroidsProvider);
  map.addLayer(centroids);
  */

  // add event listener that shows infobubble with basic information
  // about the postcode
  centroidsProvider.addEventListener('tap', function(ev) {
    var marker = ev.target;
    bubble.setPosition(marker.getPosition());
    var str = '<nobr>Postal code: ' + marker.getData().getCell('POSTAL_CODE') + '</nobr><br>' +
              'Country code: ' + marker.getData().getCell('ISO_COUNTRY_CODE') + '<br>'
    bubble.setContent(str);
    bubble.open();
  });
}





/**
 * Boilerplate map initialization code starts below:
 */

//Step 1: initialize communication with the platform
var platform = new H.service.Platform({
  app_id: 'devportal-demo-20180625',
  app_code: '9v2BkviRwi9Ot26kp2IysQ',
  useHTTPS: true
});
var pixelRatio = window.devicePixelRatio || 1;
var defaultLayers = platform.createDefaultLayers({
  tileSize: pixelRatio === 1 ? 256 : 512,
  ppi: pixelRatio === 1 ? undefined : 320
});

//Step 2: initialize a map  - not specificing a location will give a whole world view.
var map = new H.Map(document.getElementById('map'),
  defaultLayers.normal.map, {pixelRatio: pixelRatio});

map.setCenter({lat:50.892710, lng:-0.916670});
map.setZoom(12);

//Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

// Create the default UI components
var ui = H.ui.UI.createDefault(map, defaultLayers);

// create info bubble that is used to display the postcode data
bubble = new H.ui.InfoBubble(map.getCenter(), {
  content: ''
});
bubble.close();
ui.addBubble(bubble);

// Now use the map as required...
showPostcodes(map, bubble);
<script src="https://js.api.here.com/v3/3.0/mapsjs-core.js"></script>
<script src="https://js.api.here.com/v3/3.0/mapsjs-service.js"></script>
<script src="https://js.api.here.com/v3/3.0/mapsjs-ui.js"></script>
<script src="https://js.api.here.com/v3/3.0/mapsjs-mapevents.js"></script>
<link href="https://js.api.here.com/v3/3.0/mapsjs-ui.css?dp-version=1542186754" rel="stylesheet"/>

<div id="map" style="width: 100%; height: 600px; background: grey" />

我有这个 jsfiddle 演示,但我正在尝试...

  1. 仅显示包含地图中心纬度的邮政编码的边界
  2. 用颜色填充边框

链接到 jsfid -> /hectare/w0dves5j/

任何帮助将不胜感激!

【问题讨论】:

  • 请将您的代码添加到问题中。不要只是把它放在 Fiddle 中,因为该链接可能会被破坏,从而使这个问题对未来的访问者毫无用处。

标签: javascript here-api


【解决方案1】:

假设你指的是这个例子:

https://developer.here.com/api-explorer/maps-js/v3.0/platformDataExtension/postcodes-jsla-pde

  1. 您可以从 PDE 响应中遍历给定的后多边形,将它们中的每一个与地图中心进行比较,然后只显示继承此中心坐标的多边形。

  2. 多边形的填充:

    new H.map.Polygon(lineString, {
      style: {
        fillColor: '#FFFFCC',
        strokeColor: '#829',
        lineWidth: 8
      }
    

【讨论】:

  • Geocoder API 还具有检索区域形状(包括邮政编码)的能力,这可能是实现您想要做的事情的更简单方法。
  • 嗨@leopectus 你的回答也让我到了那里,但我似乎无法将 API 中的 Shape 数据转换为多边形友好数据。任何帮助将不胜感激。 jsfiddle.net/hectare/w0dves5j/22
  • 感谢您的帮助
猜你喜欢
  • 2012-01-10
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多