【发布时间】:2018-11-11 14:52:59
【问题描述】:
我无法创建一组标记并设置地图的 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>
【问题讨论】: