Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google地图群集标记数组格式_Javascript_Arrays_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript Google地图群集标记数组格式

Javascript Google地图群集标记数组格式,javascript,arrays,google-maps,google-maps-api-3,Javascript,Arrays,Google Maps,Google Maps Api 3,我有一张简单的地图,上面有来自数组的簇和标记。标记是从位置数组设置的,格式为:{lat:36.4381369,lng:-93.0502099},我有很多坐标要从其他地图添加,但它们的格式为:(36.4381369,-93.0502099) 我需要以某种方式将第一种格式转换为第二种格式。 我尝试了LatLng(36.4381369,-93.0502099)和其他组合,但标记/簇没有出现在地图上 这是可行的,但需要在每个数字前面没有lat:lang:的位置数组 function initMap()

我有一张简单的地图,上面有来自数组的簇和标记。标记是从位置数组设置的,格式为:{lat:36.4381369,lng:-93.0502099},我有很多坐标要从其他地图添加,但它们的格式为:(36.4381369,-93.0502099) 我需要以某种方式将第一种格式转换为第二种格式。 我尝试了LatLng(36.4381369,-93.0502099)和其他组合,但标记/簇没有出现在地图上

这是可行的,但需要在每个数字前面没有lat:lang:的位置数组

function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          //center: {lat: -41.850033, lng: -87.6500523}
        });
        map.setCenter(new google.maps.LatLng(35.850033, -98.6500523));

        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {zoomOnClick: false, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        {lat: 36.4381369,lng: -93.0502099},
        {lat: 36.2259742,lng: -92.6828437}
      ]
这是行不通的

 var markers = locations.map(function(location, i) {
        var point = location.maps.LatLng(location);
          return new google.maps.Marker({
            //position: new google.maps.LatLng(locations),
            //position: point,
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {zoomOnClick: false, imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
      }
      var locations = [
        (36.4381369,-93.0502099),
        (36.2259742,-92.6828437)
      ]

如果将位置更改为有效的嵌套javascript数组,则可以修改代码以将其用于创建标记:

var locations = [
  [36.4381369,-93.0502099],
  [36.2259742,-92.6828437]
];
然后像这样创建标记:

var markers = locations.map(function(location, i) {
  var pt = {lat: location[0], lng: location[1]};
  return new google.maps.Marker({
    position: pt,
    label: labels[i % labels.length]
  });
});

代码片段:

函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
缩放:4,
//中心:{lat:-41.850033,lng:-87.6500523}
});
map.setCenter(新的google.maps.LatLng(35.850033,-98.6500523));
//创建用于标记的字母字符数组。
var标签='abcdefghijklmnopqrstuvxyz';
//在地图上添加一些标记。
//注意:代码使用JavaScript Array.prototype.map()方法
//基于给定的“位置”数组创建标记数组。
//这里的map()方法与Google Maps API无关。
var markers=locations.map(函数(位置,i){
变量pt={
lat:位置[0],
液化天然气:位置[1]
};
返回新的google.maps.Marker({
职位:pt,
标签:标签[i%标签.长度]
});
});
//添加标记群集器以管理标记。
var markerCluster=新的MarkerClusterer(地图、标记、{
zoomOnClick:false,
imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
}
变量位置=[
[36.4381369, -93.0502099],
[36.2259742, -92.6828437]
]
google.maps.event.addDomListener(窗口“加载”,initMap)
html,
身体,
#地图{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}


变量位置=[(36.4381369,-93.0502099),(36.2259742,-92.6828437)]
是无效的javascript。你想解决的问题是什么?输入数据是字符串吗?这些数据来自哪里?有什么原因不能使用有效的嵌套javascript数组:
[[36.4381369,-93.0502099],[36.2259742,-92.6828437]
?我试图避免在数组中使用lat:lng:,因为我可以从其他页面复制和粘贴位置,因为它们已经是该格式的
(36.4381369,-93.0502099)
{lat:36.4381369,lng:-93.0502099}
位于代码部分的var位置。
[[36.4381369,-93.0502099],[36.2259742,-92.6828437]]
也不起作用,不返回任何标记/簇嵌套的JavaScript数组可以工作,但您必须修改代码。那么我需要修改什么以及如何修改代码?我不是很好,我从Google maps示例页面复制了代码。谢谢。这是缺少的部分,有效的嵌套数组和更改标记创建代码的内容我想不出来。谢谢,这很有效。我将您的答案标记为已接受,因为它直接回答了我的问题。现在是否可以为每个标记群集添加自定义标签和信息窗口?也许您已经在stackoverflow上的其他地方回答了类似的问题,您可以向我指出。非常感谢。