简体   繁体   English

使用 Google Maps 绘图工具绘制多边形时获取 Lat Lng

[英]Get Lat Lng when drawing a polygon with Google Maps drawing tool

I got the Google Map and Drawing Tools displayed on the map.我得到了 map 上显示的 Google Map 和绘图工具。 I can draw the shapes (circle, polygon, rectangle...) on the map.我可以在 map 上绘制形状(圆形、多边形、矩形......)。 For the start I am using the example code from Google Maps JavaScript get started page: https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/drawing-tools首先,我使用 Google Maps JavaScript 入门页面中的示例代码: https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/drawing-tools

I was able to find a lot of examples on have to draw/make a shape object by providing all the properties and LatLng.通过提供所有属性和 LatLng,我能够找到很多关于必须绘制/制作形状 object 的示例。

How do I get a LatLng for the shape that I just draw by using one of the drawing tools form Drawing Tools toolbar?如何使用绘图工具工具栏中的绘图工具之一为我刚刚绘制的形状获取 LatLng?

I am planing to make an application which will allow user to draw shapes on the map and save the LatLng in to database (i will be using MySql), so later the shape can be displayed again on the map as per request.我计划制作一个应用程序,允许用户在 map 上绘制形状并将 LatLng 保存到数据库中(我将使用 MySql),因此稍后可以根据请求在 map 上再次显示形状。 Please help.请帮忙。

There are a few ways to get the coordinates of the shapes you draw on the map.有几种方法可以获取您在 map 上绘制的形状的坐标。 Specifically for polygons you can add an event listener to the map like so.特别是对于多边形,您可以像这样向 map 添加事件侦听器。 The easiest way is to add an event listener to the map for when a polygon is finished drawing.最简单的方法是在多边形完成绘制时向 map 添加一个事件监听器。

google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
  const coords = polygon.getPath().getArray().map(coord => {
    return {
      lat: coord.lat(),
      lng: coord.lng()
    }
  });

  console.log(JSON.stringify(coords, null, 1));

  // SAVE COORDINATES HERE
});

Each type of drawing has a different format for saving so for something like circles you'd do每种类型的绘图都有不同的保存格式,所以对于像圆圈这样的东西你会做

google.maps.event.addListener(drawingManager, 'circlecomplete', function(circle) {
  const radius = circle.getRadius();

  // Save circle here
});

You also have the option of adding an event to listen for all of the events by listening to the overlaycomplete event but in that case you'd have to handle the different types inside the event.您还可以选择添加一个事件以通过侦听overlaycomplete事件来侦听所有事件,但在这种情况下,您必须处理事件中的不同类型。

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
  if (event.type == 'circle') {
    // Handle Circle using event.overlay
  }

  if (event.type == 'polygon') {
    // Handle Polygon using event.overlay
  }
});

Here's an example: https://jsfiddle.net/juop8q3n/1/这是一个例子: https://jsfiddle.net/juop8q3n/1/

And here's my sources:这是我的消息来源:

EDIT编辑

Another way I've seen people save the data is by adding an event listener to the map to get the GeoJSON data, which is a standard format for saving drawing data.我看到人们保存数据的另一种方法是向 map 添加事件侦听器以获取 GeoJSON 数据,这是保存绘图数据的标准格式。

Link: http://jsfiddle.net/y89rbfLo/链接: http://jsfiddle.net/y89rbfLo/

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

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