代码
var SAMPLE =
  '116.404,39.915,天安门\n121.474,31.230,上海外滩\n113.264,23.129,广州塔\n北京市海淀区中关村\n成都市武侯区天府广场\n杭州市西湖风景区';
var input = document.getElementById('input');

var map = new BMap.Map('map');
map.centerAndZoom(new BMap.Point(116.404, 39.915), 5);
map.enableScrollWheelZoom();

var markers = [];
var geocoder = new BMap.Geocoder();
var customIconUrl = '';
var customIconSize = null;

document.getElementById('iconFile').onchange = function (e) {
  var file = e.target.files[0];
  if (!file) return;
  customIconUrl = URL.createObjectURL(file);
  var img = new Image();
  img.onload = function () {
    var max = 32;
    var scale = Math.min(max / img.width, max / img.height, 1);
    customIconSize = new BMap.Size(Math.round(img.width * scale), Math.round(img.height * scale));
    document.getElementById('iconImg').src = customIconUrl;
    document.getElementById('iconPreview').hidden = false;
  };
  img.src = customIconUrl;
};

document.getElementById('iconReset').onclick = function () {
  customIconUrl = '';
  customIconSize = null;
  document.getElementById('iconFile').value = '';
  document.getElementById('iconPreview').hidden = true;
};

document.getElementById('btnSample').onclick = function () {
  input.value = SAMPLE;
};

document.getElementById('btnMark').onclick = function () {
  var btn = this;
  var text = input.value.trim();
  var lines = text.split('\n').filter(function (l) {
    return l.trim();
  });
  if (!lines.length) return;
  if (lines.length > 100) {
    document.getElementById('status').textContent =
      '最多支持 100 个点,当前 ' + lines.length + ' 个';
    return;
  }

  markers.forEach(function (m) {
    map.removeOverlay(m);
  });
  markers = [];
  document.getElementById('status').innerHTML = '解析中...';
  btn.disabled = true;

  var results = [];
  var pending = lines.length;

  lines.forEach(function (line, idx) {
    var parts = line.trim().split(',');
    var lng = parseFloat(parts[0]),
      lat = parseFloat(parts[1]);

    if (!isNaN(lng) && !isNaN(lat) && Math.abs(lng) <= 180 && Math.abs(lat) <= 90) {
      results[idx] = {
        point: new BMap.Point(lng, lat),
        title: parts.slice(2).join(',').trim() || '点' + (idx + 1),
      };
      if (--pending === 0) done();
    } else {
      var address = parts.length > 1 ? parts.slice(0, -1).join(',').trim() : '';
      var title = parts.length > 1 ? parts[parts.length - 1].trim() : '';
      if (!address) {
        address = line.trim();
        title = '';
      }
      (function (i, addr, t) {
        geocoder.getPoint(
          addr,
          function (point) {
            results[i] = point
              ? {point: point, title: t || addr}
              : {point: null, title: addr, error: '解析失败'};
            if (--pending === 0) done();
          },
          '',
        );
      })(idx, address, title);
    }
  });

  function done() {
    btn.disabled = false;
    var points = [],
      ok = 0,
      fail = 0,
      msgs = [];
    results.forEach(function (r, i) {
      if (!r.point) {
        fail++;
        msgs.push(i + 1 + '. ' + r.title + ' - ' + r.error);
        return;
      }
      ok++;
      points.push(r.point);
      var markerOpts = {};
      if (customIconUrl && customIconSize) {
        markerOpts.icon = new BMap.Icon(customIconUrl, customIconSize);
      }
      var marker = new BMap.Marker(r.point, markerOpts);
      if (document.getElementById('showLabel').checked) {
        var label = new BMap.Label(r.title, {offset: new BMap.Size(20, -10)});
        label.setStyle({
          border: '1px solid #ccc',
          padding: '2px 6px',
          fontSize: '12px',
          background: '#fff',
          borderRadius: '3px',
        });
        marker.setLabel(label);
      }
      map.addOverlay(marker);
      markers.push(marker);
    });
    var text = '成功 ' + ok + ' 个' + (fail ? ',失败 ' + fail + ' 个' : '');
    if (msgs.length) text += '\n' + msgs.join('\n');
    document.getElementById('status').textContent = text;
    if (points.length) map.setViewport(points, {margins: [50, 50, 50, 360]});
  }
};
预览