Google maps 谷歌地图获取坐标中心(将标签放置在多边形中心)
我使用谷歌地图,我在地图上标出如下区域:Google maps 谷歌地图获取坐标中心(将标签放置在多边形中心),google-maps,Google Maps,我使用谷歌地图,我在地图上标出如下区域: var areaCoords2 = [ new google.maps.LatLng(32.819649, 35.073102), new google.maps.LatLng(32.819604, 35.073026), new google.maps.LatLng(32.817169, 35.071321), new google.maps.LatLng(32.817097, 35.0
var areaCoords2 = [
new google.maps.LatLng(32.819649, 35.073102),
new google.maps.LatLng(32.819604, 35.073026),
new google.maps.LatLng(32.817169, 35.071321),
new google.maps.LatLng(32.817097, 35.071353),
new google.maps.LatLng(32.816042, 35.073391),
new google.maps.LatLng(32.818513, 35.075119),
new google.maps.LatLng(32.818612, 35.075054)
];
我想实现:将标签和标记放置在标记的红色区域的中间(大约)
而不是将其放置在静态纬度和经度(我有很多区域)
有一种方法可以通过编程实现吗?这里有一种方法可以满足您对Android/Java的要求。您可以尝试调整它以在web中使用:
public Location GetCentrePointFromListOfLocations(List<Location> coordList)
{
int total = coordList.size();
double X = 0;
double Y = 0;
double Z = 0;
for(Location location : coordList)
{
double lat = location.getLatitude() * Math.PI / 180;
double lon = location.getLongitude() * Math.PI / 180;
double x = Math.cos(lat) * Math.cos(lon);
double y = Math.cos(lat) * Math.sin(lon);
double z = Math.sin(lat);
X += x;
Y += y;
Z += z;
}
X = X / total;
Y = Y / total;
Z = Z / total;
double Lon = Math.atan2(Y, X);
double Hyp = Math.sqrt(X * X + Y * Y);
double Lat = Math.atan2(Z, Hyp);
Location tempLocation = new Location("");
tempLocation.setLatitude(Lat * 180 / Math.PI);
tempLocation.setLongitude(Lon * 180 / Math.PI);
return tempLocation;
}
公共位置GetCentrePointFromListOfLocations(List coordList)
{
int total=coordList.size();
双X=0;
双Y=0;
双Z=0;
用于(位置:coordList)
{
双纬度=location.getLatitude()*Math.PI/180;
double lon=location.getLongitude()*Math.PI/180;
双x=数学cos(lat)*数学cos(lon);
双y=数学cos(lat)*数学sin(lon);
双z=数学sin(lat);
X+=X;
Y+=Y;
Z+=Z;
}
X=X/总数;
Y=Y/总数;
Z=Z/总数;
double Lon=Math.atan2(Y,X);
双Hyp=数学sqrt(X*X+Y*Y);
双Lat=数学atan2(Z,Hyp);
位置模板位置=新位置(“”);
模板位置设置纬度(纬度*180/数学π);
设置经度(Lon*180/Math.PI);
返回模板定位;
}
您添加了什么
zoom: 10,
center: myLatlng,
为您标记对象
(或)
下面的代码构造一个google.maps.Polygon,并在其边界的中心放置一个MapLabel
// Construct the polygon.
var mypolygon2 = new google.maps.Polygon({
paths: polyCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35
});
mypolygon2.setMap(map);
//Define position of label
var bounds = new google.maps.LatLngBounds();
for (var i=0; i< polyCoords.length; i++) {
bounds.extend(polyCoords[i]);
}
var myLatlng = bounds.getCenter();
var mapLabel2 = new MapLabel({
text: '2',
position: myLatlng,
map: map,
fontSize: 20,
align: 'left'
});
mapLabel2.set('position', myLatlng);
var obj = {};
obj.poly = mypolygon2;
obj.label = mapLabel2;
多边形阵列
这一条适用于我的动态多边形值。
//本例创建了一个表示百慕大三角形的简单多边形。
var gpolygons=[];
函数initStaticMap(){
var bounds=new google.maps.LatLngBounds();
var latlng=new google.maps.latlng(-22.5747697,-43.857650);/-22.820554842103107--43.184738187119365
var map=new google.maps.map(document.getElementById('map_canvas'){
缩放:10,
中心:拉特林,
mapTypeId:google.maps.mapTypeId.ROADMAP,
});
//定义多边形路径的板条坐标。
var-coord=;
//构造多边形。
var poly=new google.maps.Polygon({
路径:coord,
strokeColor:“#FF0000”,
笔划不透明度:0.8,
冲程重量:2,
填充颜色:'#FF0000',
不透明度:0.35
});
poly.setMap(map);
//var center=poly.getBounds().getCenter();
//fitBounds(poly.getBounds());
//定义标签的位置
var bounds=new google.maps.LatLngBounds();
对于(变量i=0;i
,将标签放置在多边形的中心。另一种方法(适用于任何形状)。。。它给出边界的中心,而不是多边形的中心
// Construct the polygon.
var mypolygon2 = new google.maps.Polygon({
paths: polyCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35
});
mypolygon2.setMap(map);
//Define position of label
var bounds = new google.maps.LatLngBounds();
for (var i=0; i< polyCoords.length; i++) {
bounds.extend(polyCoords[i]);
}
var myLatlng = bounds.getCenter();
var mapLabel2 = new MapLabel({
text: '2',
position: myLatlng,
map: map,
fontSize: 20,
align: 'left'
});
mapLabel2.set('position', myLatlng);
var obj = {};
obj.poly = mypolygon2;
obj.label = mapLabel2;
<script type="text/javascript" src="https://cdn.rawgit.com/googlemaps/js-map-label/gh-pages/src/maplabel.js"></script>
<script>
// This example creates a simple polygon representing the Bermuda Triangle.
var gpolygons = [];
function initStaticMap() {
var bounds = new google.maps.LatLngBounds();
var latlng = new google.maps.LatLng(-22.5747697,-43.857650); //-22.820554842103107--43.184738187119365
var map = new google.maps.Map(document.getElementById('map_canvas'), {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
});
// Define the LatLng coordinates for the polygon's path.
<?php if($store_delivery_zone){ foreach($store_delivery_zone as $point){ ?>
var coord<?php echo $point['delivery_zone_id']; ?> = <?php echo $point['polygon_points']; ?>;
// Construct the polygon.
var poly<?php echo $point['delivery_zone_id']; ?> = new google.maps.Polygon({
paths: coord<?php echo $point['delivery_zone_id']; ?>,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35
});
poly<?php echo $point['delivery_zone_id']; ?>.setMap(map);
//var center = poly<?php echo $point['delivery_zone_id']; ?>.getBounds().getCenter();
//map.fitBounds(poly<?php echo $point['delivery_zone_id']; ?>.getBounds());
//Define position of label
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < coord<?php echo $point['delivery_zone_id']; ?>.length; i++) {
bounds.extend(coord<?php echo $point['delivery_zone_id']; ?>[i]);
}
var myLatlng<?php echo $point['delivery_zone_id']; ?> = bounds.getCenter();
var mapLabel<?php echo $point['delivery_zone_id']; ?> = new MapLabel({
text: '<?php echo $point['delivery_cost']; ?>',
position: myLatlng<?php echo $point['delivery_zone_id']; ?>,
map: map,
fontSize: 20,
align: 'left'
});
mapLabel<?php echo $point['delivery_zone_id']; ?>.set('position', myLatlng<?php echo $point['delivery_zone_id']; ?>);
var obj = {};
obj.poly = poly<?php echo $point['delivery_zone_id']; ?>;
obj.label = mapLabel<?php echo $point['delivery_zone_id']; ?>;
gpolygons.push(obj);
<?php } } ?>
google.maps.event.addListener(map, "idle", function()
{
google.maps.event.trigger(map, 'resize');
});
}
</script>