计算纬度和经度坐标的中心

计算纬度和经度坐标的中心

本文介绍了计算纬度和经度坐标的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个优雅的解决方案,它能够计算多边形)。



表: locations :

  id |城市|纬度|经度
--------------------------------------------- -
1 |柏林| 52.524268 | 13.406290
--------------------------------------------- -
2 |伦敦| 51.508129 | -0.1280050
-------------------------------------------- ---
3 |汉堡| 53.551084 | 9.9936817
--------------------------------------------- -
4 |阿姆斯特丹| 52.370215 | 4.8951678
--------------------------------------------- -

目前的计算:

  function calculateCenter($ array_locations){

$ minlat = false;
$ minlng = false;
$ maxlat = false;
$ maxlng = false;

foreach($ array_locations as $ geolocation){

if($ minlat === false){$ minlat = $ geolocation ['lat']; } else {$ minlat =($ geolocation ['lat']< $ minlat)? $ geolocation ['lat']:$ minlat; }
if($ maxlat === false){$ maxlat = $ geolocation ['lat']; }其他{$ maxlat =($ geolocation ['lat']> $ maxlat)? $ geolocation ['lat']:$ maxlat; }
if($ minlng === false){$ minlng = $ geolocation ['lon']; }其他{$ minlng =($ geolocation ['lon']< $ minlng)? $ geolocation ['lon']:$ minlng; }
if($ maxlng === false){$ maxlng = $ geolocation ['lon']; }其他{$ maxlng =($ geolocation ['lon']> $ maxlng)? $ geolocation ['lon']:$ maxlng; }
}

//计算中心
$ lat = $ maxlat - (($ maxlat - $ minlat)/ 2);
$ lon = $ maxlng - (($ maxlng - $ minlng)/ 2);

返回数组($ lat,$ lon);


解决方案

您可以使用方法和方法。

我重新排列了坐标以形成一个凸多边形(全部顶点指向向外,远离中心)。通过将第一个坐标作为第一个和最后一个值放在 polygonCoords 数组中,关闭多边形。



请参阅

  var map; 
var polygon;
var bounds = new google.maps.LatLngBounds();
var i;
var myLatLng = new google.maps.LatLng(52.5,6.6);
var myOptions = {
zoom:5,
center:myLatLng,
mapTypeId:google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById(map_canvas),
myOptions);

var polygonCoords = [
new google.maps.LatLng(52.524268,13.406290),
new google.maps.LatLng(53.551084,9.9936817),
google .maps.LatLng(51.508129,-0.1280050),
new google.maps.LatLng(52.370215,4.8951678),
new google.maps.LatLng(52.524268,13.406290)//开始&终点
];

polygon = new google.maps.Polygon({
paths:polygonCoords,
strokeColor:#FF0000,
strokeOpacity:0.8,
strokeWeight :3,
fillColor:#FF0000,
fillOpacity:0.05
});
polygon.setMap(map);

for(i = 0; i< polygonCoords.length; i ++){
bounds.extend(polygonCoords [i]);
}

//多边形的中心
var latlng = bounds.getCenter();

var marker = new google.maps.Marker({
position:latlng,
map:map,
title:latlng.toString()
});


I'm looking for a elegant solution that calculates the center between several polygon).

Table: locations:

id |    city    |   latitude   |  longitude
-----------------------------------------------
1  |   Berlin   |   52.524268  |   13.406290
-----------------------------------------------
2  |   London   |   51.508129  |  -0.1280050
-----------------------------------------------
3  |   Hamburg  |   53.551084  |   9.9936817
-----------------------------------------------
4  |  Amsterdam |   52.370215  |   4.8951678
-----------------------------------------------

The current calculation:

function calculateCenter($array_locations) {

    $minlat = false;
    $minlng = false;
    $maxlat = false;
    $maxlng = false;

    foreach ($array_locations as $geolocation) {

         if ($minlat === false) { $minlat = $geolocation['lat']; } else { $minlat = ($geolocation['lat'] < $minlat) ? $geolocation['lat'] : $minlat; }
         if ($maxlat === false) { $maxlat = $geolocation['lat']; } else { $maxlat = ($geolocation['lat'] > $maxlat) ? $geolocation['lat'] : $maxlat; }
         if ($minlng === false) { $minlng = $geolocation['lon']; } else { $minlng = ($geolocation['lon'] < $minlng) ? $geolocation['lon'] : $minlng; }
         if ($maxlng === false) { $maxlng = $geolocation['lon']; } else { $maxlng = ($geolocation['lon'] > $maxlng) ? $geolocation['lon'] : $maxlng; }
    }

    // Calculate the center
    $lat = $maxlat - (($maxlat - $minlat) / 2);
    $lon = $maxlng - (($maxlng - $minlng) / 2);

    return array($lat, $lon);
}
解决方案

As you are using Google Maps you can use getBounds() method and getCenter() method.

I have rearranged your coordinates to form a Convex Polygon (All the vertices point 'outwards', away from the center).The polygon is closed by having the first coordinate as the first and last value in polygonCoords array.

See jsfiddle

var map;
var polygon;
var bounds = new google.maps.LatLngBounds();
var i;
var myLatLng = new google.maps.LatLng(52.5,6.6);
var myOptions = {
  zoom: 5,
  center: myLatLng,
  mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

var polygonCoords = [
    new google.maps.LatLng(52.524268,13.406290),
    new google.maps.LatLng(53.551084,9.9936817),
    new google.maps.LatLng(51.508129,-0.1280050),
    new google.maps.LatLng(52.370215,4.8951678),
    new google.maps.LatLng(52.524268,13.406290)//Start & end point
    ];

polygon = new google.maps.Polygon({
   paths: polygonCoords,
   strokeColor: "#FF0000",
   strokeOpacity: 0.8,
   strokeWeight: 3,
   fillColor: "#FF0000",
   fillOpacity: 0.05
 });
 polygon.setMap(map);

for (i = 0; i < polygonCoords.length; i++) {
   bounds.extend(polygonCoords[i]);
}

// The Center of the polygon
var latlng = bounds.getCenter();

var marker = new google.maps.Marker({
  position: latlng,
  map: map,
  title:latlng.toString()
});

这篇关于计算纬度和经度坐标的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:26