dongshi2141 2015-09-27 22:42
浏览 89

在谷歌地图API v3 javascript上绘制多个多边形

I have several coordinate data saved on my database. The data looked like this:

[
    [-8.411328650721382, 115.2549934387207],
    [-8.423385320191324, 115.26924133300781],
    [-8.428988996455418, 115.27507781982422],
    [-8.445120338164106, 115.2682113647461],
    [-8.452082077411186, 115.2656364440918],
    [-8.46430726648532, 115.26580810546875],
    [-8.470419715493609, 115.26615142822266],
    [-8.476022708444539, 115.26477813720703],
    [-8.481116267546547, 115.25671005249023],
    [-8.492491639030948, 115.24469375610352],
    [-8.481795403659492, 115.2414321899414],
    [-8.472287388879758, 115.24641036987305],
    [-8.462609346758175, 115.24606704711914],
    [-8.455308206642881, 115.24417877197266],
    [-8.445629737978635, 115.23714065551758],
    [-8.434252981999162, 115.2352523803711],
    [-8.422026840993878, 115.23490905761719],
    [-8.410479575344654, 115.23954391479492],
    [-8.405385084022475, 115.24383544921875]
]

I have to draw several polygon on map but when I do, all the polygons are connected each other.

Here is my javascript:

<script>

var map;
var i = 0;
var poly = {};
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    center: {lat: -1.55038056899795, lng: 118.6962890625},
    zoom: 5,
    disableDefaultUI: true
  });
  var jsonPeta = <?=$peta?>;
  var coordArray = [];
  var path = [];

  for(i=0;i<jsonPeta.length;i++){
    //console.log(i);
    coordArray = JSON.parse(jsonPeta[i].peta);
    coordArray.forEach(function(entry){
      path.push(new google.maps.LatLng(entry[0],entry[1]));
    });
    poly[i] = new google.maps.Polygon({
    paths: path,
    editable: false,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#03a9f4',
      clickable: true,
      fillOpacity: 0.35
  });
  poly[i].setMap(map);
  console.log(poly[i]);
  }

  var input = document.getElementById('pac-input');

  var searchBox = new google.maps.places.SearchBox(input);
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);

  map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
  });

  var markers = [];
  searchBox.addListener('places_changed', function() {
    var places = searchBox.getPlaces();

if (places.length == 0) {
  return;
}

// Clear out the old markers.
markers.forEach(function(marker) {
  marker.setMap(null);
});
markers = [];

// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
  var icon = {
    url: place.icon,
    size: new google.maps.Size(71, 71),
    origin: new google.maps.Point(0, 0),
    anchor: new google.maps.Point(17, 34),
    scaledSize: new google.maps.Size(25, 25)
  };

  // Create a marker for each place.
  markers.push(new google.maps.Marker({
    map: map,
    icon: icon,
    title: place.name,
    position: place.geometry.location
    //document.getElementById("lat").value = place.geometry.Latitude
  }));
  document.getElementById('lat').value = place.geometry.location.lat();
  document.getElementById('lon').value = place.geometry.location.lng();
  if (place.geometry.viewport) {
    // Only geocodes have viewport.
    bounds.union(place.geometry.viewport);
  } else {
    bounds.extend(place.geometry.location);
  }
});
map.fitBounds(bounds);


});
}
</script>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图