Perpendicular of a line in PHP
Closed this issue · 1 comments
evigra commented
Good day friend.
How can I make a perpendicular of a line in PHP?
In JAVASCRIPT (gmaps)
`
var spherical = google.maps.geometry.spherical;
var F = new google.maps.LatLng(51.524, -0.099);
var T = new google.maps.LatLng(51.531, -0.114);
var myOptions = {
zoom: 14,
center: F,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// Draw line
var segmentCoordinates = [F, T ];
var segment = new google.maps.Polyline({
path: segmentCoordinates,
geodesic: true,
strokeColor: '#00FF00',
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
});
// Get direction of the segment
var heading = spherical.computeHeading(F, T);
var dist = 500; // distance in meters
var A = spherical.computeOffset(F, dist, heading+90);
var B = spherical.computeOffset(F, dist, heading-90);
var perpendicularCoordinates = [A, B ];
var perpendicular = new google.maps.Polyline({
path: perpendicularCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
});
`
alexpechkarev commented
Hi @evigra,
Thank you for your question.
This package has SphericalUtil class that can be used to obtain values for
// JavaScript
var heading = spherical.computeHeading(F, T);
var A = spherical.computeOffset(F, dist, heading+90);
var B = spherical.computeOffset(F, dist, heading-90);
// PHP
$distance = 500;
$from = ['lat' => 25.775, 'lng' => -80.190];
$to = ['lat' => 21.774, 'lng' => -80.190];
$heading = SphericalUtil ::computeHeading($from, $to);
$A = SphericalUtil::computeOffset($from, $distance, $heading+90);
$B = SphericalUtil ::computeOffset($to, $distance, $heading-90);
Haven't tried above code, but it should be not far from what you looking for.
Regards,
Alexander