# javascript - Google Maps - point distance from polyline

Get the solution ↓↓↓I have a some google maps polylines. I'm trying to draw 2 polylines around them, so they make some kind of boundary:

So for each point of my original polyline I calculate the boundary line point, so it is lets say 25 meters from the original point:

```
function getOffsetPoints($p1, $p2, $p3, $distance = 25) {
$gap = (180 / M_PI) * ($distance / 6378137);
$sx = $p3['lat'] - $p1['lat'];
$sy = $p3['lng'] - $p1['lng'];
$cx = $p2['lat']; $cy = $p2['lng'];
$normx = $sy; $normy = -$sx;
$length = sqrt($normx * $normx + $normy * $normy);
$normx = $normx / $length; $normy = $normy / $length;
$newx1 = $cx - $normx * $gap;
$newy1 = $cy - $normy * $gap;
$newx2 = $cx + $normx * $gap;
$newy2 = $cy + $normy * $gap;
return [
['lat'=>$newx1, 'lng'=>$newy1],
['lat'=>$newx2, 'lng'=>$newy2]
];
}
```

For this example lets leave out the first and last point of the polyline. So I always look at the previous and next point, and calculate the offset of the center point.

I think it is working pretty good. When I plot out my original and newly calculated points, they are in a equal distance from the original point:

The black ones are the original points and the blue and red are the offset points.

Now I take all the data and draw it in a google map using polylines:

```
for (var i=0; i<offset_polylines.length; i++) {
var oPath = offset_polylines[i];
var polyline = new google.maps.Polyline({
path: offset_polylines,
strokeColor: "#ffffff",
strokeOpacity: 1,
strokeWeight: 1,
map: map,
});
}
```

In the result you can see, that the lines are clearly not in a same distance from the original yellow line. What's happening here? The more the line goes 'vertical' the less is the distance. Why is that? If I measure the distance on the map the difference between my 'horizontal line' on the picture and the 'vertical' one is 10 meters.

### Answer

#### Solution:

Note that that ratio meters/degree is constant for latitude (N/S direction, along meridian), but changes for longitude direction depending on latitude with coefficient`Abs(Cos(Latitude))`

, so 1 degree along parallel at equator contains 111 km, but for 45 degree of nothern latitude - only 78 km.

Your calculation is rough approximation, so you can perhaps just multiply longitude differences (`$sx`

) by coefficient`1/Abs(Cos($lat))`

to diminish anisothropy.

More exact approach might include computation of coordinates by distance and azimuth as described here. Don't forget about degrees and radians.

```
dlng = lng3 - lng1
bearing = atan2( sin(dlng)⋅cos(lat3), cos(lat1)⋅sin(lat3) − sin(lat1)⋅cos(lat3)⋅cos(dlng))
perpbearing1 = bearing + Pi/2
perpbearing2 = bearing - Pi/2
latperp = asin(sin(lat2)⋅cos(d/R) + cos(lat2)⋅sin(d/R)⋅ cos(perbearingX))
lonperp = lon2+atan2(sin(perpbearingX)⋅sin(d/R)⋅cos(lat2),cos(d/R)−sin(lat2)⋅sin(latperp))
```

**Share solution ↓**

### Additional Information:

**Date the issue was resolved:**

Link To Answer People are also looking for solutions of the problem: uncaught mysqli_sql_exception

### Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

### Similar questions

Find the answer in similar questions on our website.

### Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.