laravel GoogleMap上的Store Circle数据[已关闭]

kokeuurv  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(101)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

20天前关门了。
这篇文章是编辑并提交审查20天前。
Improve this question
我在一个Laravel应用程序中工作,在Google Map上绘制的多边形坐标存储在一个数据类型为多边形的列中。


的数据
稍后下面的代码用于搜索这些多边形内的坐标。

$point = new Point( $request->latitude, $request->longitude );
$zone  = RestaurantZones::contains( 'coordinates', $point )->pluck( 'id' );

字符串
现在,我想存储在谷歌Map上绘制的圆数据,并以类似的方式搜索圆内的坐标。

xienkqul

xienkqul1#

在一个圆中,你可以通过创建两个字段纬度和经度来存储它,或者正如你提到的坐标,这里也需要存储圆的半径,以确定坐标是否在圆内。让我们来演示一下。

绘制和存储圆的坐标和半径

let map;
let drawingManager;
let circle;

function initMap() {
  map = new google.maps.Map(document.getElementById("map"), {
    center: { lat: -34.397, lng: 150.644 },
    zoom: 8,
  });

  drawingManager = new google.maps.drawing.DrawingManager({
    drawingControl: true,
    drawingControlOptions: {
      position: google.maps.ControlPosition.TOP_CENTER,
      drawingModes: [google.maps.drawing.OverlayType.CIRCLE],
    },
    circleOptions: {
      fillColor: "#ffff00",
      fillOpacity: 0.5,
      strokeWeight: 2,
      clickable: false,
      editable: true,
      zIndex: 1,
    },
  });

  drawingManager.setMap(map);

  google.maps.event.addListener(drawingManager, "circlecomplete", function (circleObject) {
    // Remove the previous circle if it exists
    if (circle) {
      circle.setMap(null);
    }

    // Set the new circle
    circle = circleObject;

    // Log coordinates and radius
    const center = circle.getCenter();
    const radius = circle.getRadius();
    console.log("Coordinates: " + center.toUrlValue());
    console.log("Radius: " + radius + " meters");

  // Send the data to the Laravel controller using AJAX
  $.ajax({
    url: 'your-route',
    method: 'POST',
    data: {
      center: center.toUrlValue(),
      radius: radius,
    },
    success: function (response) {
      console.log(response.message);
    },
    error: function (error) {
      console.error('Error:', error);
    },
  });

  });
}

window.initMap = initMap;
#map {
  height: 100%;
}

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<html>
  <head>
    <title>Drawing Tools</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>

  </head>
  <body>
    <div id="map"></div>

    <script src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap&libraries=drawing&v=weekly" async defer></script>
  </body>
</html>

当你以空间的方式存储坐标时,可以使用它来确定坐标是否在圆内。

$targetPoint = new Point($latitude, $longitude);
$zone = RestaurantZones::whereRaw(
        "ST_Distance(coordinates, ?) <= radius",
        [$targetPoint]
    )->first();

    if ($zone) {
        return 'Coordinate is inside a zone';
    }

    return 'Coordinate is outside all zones';


如果坐标是在lat和lat分开的,那么你可以用这个

public function checkCoordinateInZone($latitude, $longitude)
{
    $targetPoint = ['lat' => $latitude, 'lng' => $longitude];
    
    $zones = RestaurantZones::all();
    
    foreach ($zones as $zone) {
        $circleCenter = $zone->coordinates;
        $circleRadius = $zone->radius;
        
        $distance = $this->calculateDistance($circleCenter, $targetPoint);
        
        if ($distance <= $circleRadius) {
            return 'Coordinate is inside a zone';
        }
    }
    
    return 'Coordinate is outside all zones';
}

private function calculateDistance($point1, $point2)
{
    $lat1 = deg2rad($point1->getLat());
    $lon1 = deg2rad($point1->getLng());
    $lat2 = deg2rad($point2['lat']);
    $lon2 = deg2rad($point2['lng']);

    // Haversine formula to calculate the distance between two coordinates
    $dlat = $lat2 - $lat1;
    $dlon = $lon2 - $lon1;
    $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlon / 2) * sin($dlon / 2);
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
    $distance = 6371000 * $c; // Radius of Earth in meters

    return $distance;
}

希望这能帮助您更好地理解Google Map JS API。

相关问题