这里地图:无法从标记组范围设置ViewBounds

时间:2018-11-11 14:53:00

标签: maps here-api

我无法创建一组标记,也无法设置地图的viewBouns来查看该组中的所有标记。我看到的问题是在地图上设置了默认位置(美国的某个地方),根本看不到任何标记。最后,我希望从[txt]数组中的城市列表中创建所有标记。这是代码:

<script>
      // Initialize the platform object:
      var platform = new H.service.Platform({
      'app_id': 'xxxx',
      'app_code': 'xxxx'
      });

      // Obtain the default map types from the platform object
      var maptypes = platform.createDefaultLayers();

      // Instantiate (and display) a map object:
      var map = new H.Map(
        document.getElementById('mapContainer'),
        maptypes.normal.map,
        {
          zoom: 16,
          center: { lng: 51, lat: 12 }
        }
      );

      // TODO: here must be the code which loads the database 
      // with addresses to the txt variable
      var txt = [
          'Blagoevgrad, Bulgaria',
          'Burgas, Bulgaria',
          'Varna, Bulgaria',
          'Veliko Tarnovo, Bulgaria',
          'Vidin, Bulgaria',
          'Vratsa, Bulgaria',
          'Gabrovo, Bulgaria',
          'Dobrich, Bulgaria',
          'Kardzhali, Bulgaria',
          'Kiystendil, Bulgaria',
          'Lovech, Bulgaria',
          'Montana, Bulgaria',
          'Pazardzik, Bulgaria',
          'Pernik, Bulgaria',
          'Pleven, Bulgaria',
          'Plovdiv grad, Bulgaria',
          'Plovdiv oblast, Bulgaria',
          'Razgrad, Bulgaria',
          'Ruse, Bulgaria',
          'Silistra, Bulgaria',
          'Sliven, Bulgaria',
          'Smolian, Bulgaria',
          'Stara Zagora, Bulgaria',
          'Targovishte, Bulgaria',
          'Haskovo, Bulgaria',
          'Shumen, Bulgaria',
          'Yambol, Bulgaria'];

      group = new H.map.Group();

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

      // Enable the event system on the map instance:
      var mapEvents = new H.mapevents.MapEvents(map);

      // Add event listeners:
      map.addEventListener('tap', function(evt) {
        // Log 'tap' and 'mouse' events:
        console.log(evt.type, evt.currentPointer.type); 
      });

      // Instantiate the default behavior, providing the mapEvents object: 
      var behavior = new H.mapevents.Behavior(mapEvents);

      var geocodingParams = [ ];
      var parseList = function prsLst(list) {

      var i;
      for (i=0; i<txt.length; i++) {
        geocodingParams.push({searchText:txt[i]});
      }
      };

      // Define a callback function to process the geocoding response:
      var onResult = function(result) {
        var locations = result.Response.View[0].Result,
          position,
          marker;
        // Add a marker for each location found
        for (i = 0;  i < locations.length; i++) {
        position = {
          lat: locations[i].Location.DisplayPosition.Latitude,
          lng: locations[i].Location.DisplayPosition.Longitude
        };
        marker = new H.map.Marker(position);
        map.addObject(marker);
        //map.setCenter(position);
        group.addObjects([marker]);
        }
        map.setViewBounds(group.getBounds());
      };

      // Get an instance of the geocoding service:
      var geocoder = platform.getGeocodingService();

      // Call the geocode method with the geocoding parameters,
      // the callback and an error callback function (called if a
      // communication error occurs):
      var searchAll = function srchAll() {
        var i=0;
        for (i=0; i<geocodingParams.length; i++)
        geocoder.geocode(geocodingParams[i], onResult, function(e) {
          alert(e);
        });

      };
      parseList();
      searchAll();

    </script>

1 个答案:

答案 0 :(得分:0)

您必须添加要映射的组并将标记添加到组

// Initialize the platform object:
var platform = new H.service.Platform({
     'app_id': 'xxxx',
     'app_code': 'xxxx'
});

// Obtain the default map types from the platform object
var maptypes = platform.createDefaultLayers();

// Instantiate (and display) a map object:
var map = new H.Map(
     document.getElementById('mapContainer'),
     maptypes.normal.map,
     {
          zoom: 16,
          center: { lng: 51, lat: 12 }
     }
);

// TODO: here must be the code which loads the database
// with addresses to the txt variable
var txt = [
     'Blagoevgrad, Bulgaria',
     'Burgas, Bulgaria',
     'Varna, Bulgaria',
     'Veliko Tarnovo, Bulgaria',
     'Vidin, Bulgaria',
     'Vratsa, Bulgaria',
     'Gabrovo, Bulgaria',
     'Dobrich, Bulgaria',
     'Kardzhali, Bulgaria',
     'Kiystendil, Bulgaria',
     'Lovech, Bulgaria',
     'Montana, Bulgaria',
     'Pazardzik, Bulgaria',
     'Pernik, Bulgaria',
     'Pleven, Bulgaria',
     'Plovdiv grad, Bulgaria',
     'Plovdiv oblast, Bulgaria',
     'Razgrad, Bulgaria',
     'Ruse, Bulgaria',
     'Silistra, Bulgaria',
     'Sliven, Bulgaria',
     'Smolian, Bulgaria',
     'Stara Zagora, Bulgaria',
     'Targovishte, Bulgaria',
     'Haskovo, Bulgaria',
     'Shumen, Bulgaria',
     'Yambol, Bulgaria'];

group = new H.map.Group();

//DevBab: Add group to the map
map.addObject(group);

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

// Enable the event system on the map instance:
var mapEvents = new H.mapevents.MapEvents(map);

// Add event listeners:
map.addEventListener('tap', function (evt) {
     // Log 'tap' and 'mouse' events:
     console.log(evt.type, evt.currentPointer.type);
});

// Instantiate the default behavior, providing the mapEvents object:
var behavior = new H.mapevents.Behavior(mapEvents);

var geocodingParams = [];
var parseList = function prsLst(list) {

     var i;
     for (i = 0; i < txt.length; i++) {
          geocodingParams.push({ searchText: txt[i] });
     }
};

// Define a callback function to process the geocoding response:
var onResult = function (result) {
     var locations = result.Response.View[0].Result,
          position,
          marker;
     // Add a marker for each location found
     for (i = 0; i < locations.length; i++) {
          position = {
               lat: locations[i].Location.DisplayPosition.Latitude,
               lng: locations[i].Location.DisplayPosition.Longitude
          };
          marker = new H.map.Marker(position);
          // DevBab : don't add markers to the map but to the grop
          // map map.addObject(marker);

          //map.setCenter(position);
          group.addObjects([marker]);
     }
     map.setViewBounds(group.getBounds());
};

// Get an instance of the geocoding service:
var geocoder = platform.getGeocodingService();

// Call the geocode method with the geocoding parameters,
// the callback and an error callback function (called if a
// communication error occurs):
var searchAll = function srchAll() {
     var i = 0;
     for (i = 0; i < geocodingParams.length; i++)
          geocoder.geocode(geocodingParams[i], onResult, function (e) {
               alert(e);
          });

};
parseList();
searchAll();