【发布时间】:2017-11-30 20:38:39
【问题描述】:
我有简单的地图,其中包含数组中的集群和标记。标记是从位置数组中设置的,格式为:{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)
]
【问题讨论】:
-
var locations = [(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 数组工作,但您必须修改代码。
-
那么我需要什么以及如何修改代码?我不是很好,我从谷歌地图示例页面复制了代码。谢谢。
标签: javascript arrays google-maps google-maps-api-3