开发者

Google Maps Polyline: calculate endpoint from original point

开发者 https://www.devze.com 2023-02-13 06:12 出处:网络
I want to draw a polyline using Google Maps. I have read the API and did some research, but i am still left with a fundamental question.

I want to draw a polyline using Google Maps.

I have read the API and did some research, but i am still left with a fundamental question.

Information:

lat: 63.43243500
lon: 10.37045667
angle: 230 degrees (0 = north)

How can i make a polyline that is 60 meters lon开发者_如何学Gog that originates from the original lat / lon with a angle of 230 degrees?


You need to calculate the endpoint of the line in (lat,lon). This can be done using the calculateOffset function in the google.maps.geometry.spherical namespace. It has three required parameters: the starting lat/lng point, the distance to travel, and the heading angle.

Here's an example for your situation:

var startLL = new google.maps.LatLng(63.43243500,10.37045667);
var endLL = new google.maps.geometry.spherical.computeOffset(startLL, 60, 230);

From the Polyline example, you can create a Polyline from these points as:

var coordinates = [startLL, endLL];
var path = new google.maps.Polyline({
  path: coordinates,
  strokeColor: "#FF0000",
  strokeOpacity: 1.0,
  strokeWeight: 2
});

Update: You also need to make sure that you include the geometry library, which is not included by default. Following the instructions here, you need to change your bootstrap request to:

http://maps.google.com/maps/api/js?libraries=geometry&sensor=false
0

精彩评论

暂无评论...
验证码 换一张
取 消