简体   繁体   English

让用户在Google地图上绘图?

[英]Letting User Draw On a Google Map?

Does anyone know if it's possible to allow a user to draw a line from Point A to point B on a Google map? 有谁知道是否可以允许用户在谷歌地图上从A点到B点画一条线?

If so, how is this usually done? 如果是这样,通常如何做?

Look at the documentation on polylines . 查看折线的文档。 You just need to add an 'click' event on the map and handle the drawing of the lines. 您只需在地图上添加“点击”事件并处理线条的绘制。

You can use the Polylines and specify two points or more using gooogle.maps.LatLng. 您可以使用折线,并使用gooogle.maps.LatLng指定两个或更多点。 Here is a JSFiddle Demo : 这是一个JSFiddle演示

  //create points on the Polylines
  var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];

  //create the Polyline and feed it points with stroke styling
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  //set the polylines on the map
  flightPath.setMap(map);

To be much more detail as others suggests, the coodinates can be either entered by user either by a click event to place markers as points on the map and then supply the markers coodinates as points for the polylines or you can also create a form where the users can manually enter their address or their lat lng as points to draw the points, but the detail way of drawing the polylines is similar to the above example except Lat Lng are statically coded on the demo. 要像其他人所说的那样更加细节,可以通过点击事件将用户输入,将标记作为点放置在地图上,然后将标记坐标作为折线的点提供,或者您也可以创建一个表格。用户可以手动输入他们的地址或他们的lat lng作为绘制点的点,但绘制折线的详细方法与上面的示例相似,只是Lat Lng在演示中静态编码。

They have sample code which allows you to do this(to see the code simply click view source): 他们有示例代码,允许您执行此操作(只需单击查看源代码即可查看代码):

http://code.google.com/apis/maps/documentation/javascript/examples/polyline-complex.html http://code.google.com/apis/maps/documentation/javascript/examples/polyline-complex.html

  1. Wire an onclick event to your map. 将onclick事件连接到地图。
  2. Add a point to your polyline(the line the use is drawing) by looking at the event.LatLng object to get the Latitude and Longitude. 通过查看event.LatLng对象来获取纬度和经度,从而为折线(用户正在绘制的线)添加一个点。
  3. When the user clicks again the polyline will automatically update the rendering when the new point has been pushed onto the array it renders from. 当用户再次单击时,折线将在新点被推送到其渲染的阵列上时自动更新渲染。

I am going to be posting the solution on this test page: http://www.comehike.com/draggable_marker.php 我将在此测试页面上发布解决方案: http//www.comehike.com/draggable_marker.php

At the time of this answer, the solution is not there, but hopefully very soon I will get this working. 在这个答案的时候,解决方案不存在,但希望很快我会得到这个工作。

Yes it is. 是的。 Check out MapMyRun.com for a great example of a use case. 查看MapMyRun.com以获取用例的一个很好的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM