本文介绍了如何从gmaps RECTANGLE(叠加)获得4个顶点坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 gmaps RECTANGLE (Overlay) 获得 4 个顶点坐标?

How do you get 4 vertex coordinates from gmaps RECTANGLE (Overlay)?

我只能从 selectedShape.getBounds()

我需要一个 4 顶点矩形来对 MySQL php 函数进行后期处理.

I need a 4 vertex rectangle to post-process check in MySQL php functions.

推荐答案

NorthWest = NorthEast Lat , SouthWest Lng

NorthWest = NorthEast Lat , SouthWest Lng

SouthEast = SouthWest Lat , NorthEast Lng

SouthEast = SouthWest Lat , NorthEast Lng

var bounds = rectangle.getBounds();
var NE = bounds.getNorthEast();
var SW = bounds.getSouthWest();
// North West
var NW = new google.maps.LatLng(NE.lat(),SW.lng());
// South East
var SE = new google.maps.LatLng(SW.lat(),NE.lng());

示例小提琴

代码片段:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
    var rectangle = new google.maps.Rectangle({
      bounds: map.getBounds()
    })
    var bounds = rectangle.getBounds();
    var NE = bounds.getNorthEast();
    var NEmark = new google.maps.Marker({
      map: map,
      position: NE,
      title: "NE"
    });
    var SW = bounds.getSouthWest();
    var SWmark = new google.maps.Marker({
      map: map,
      position: SW,
      title: "SW"
    });
    // North West
    var NW = new google.maps.LatLng(NE.lat(), SW.lng());
    var NWmark = new google.maps.Marker({
      map: map,
      position: NW,
      title: "NW"
    });
    // South East
    var SE = new google.maps.LatLng(SW.lat(), NE.lng());
    var SEmark = new google.maps.Marker({
      map: map,
      position: SE,
      title: "SE"
    });

    var polygon = new google.maps.Polygon({
      map: map,
      paths: [
        [NE, NW, SW, SE]
      ]
    });
    map.setZoom(map.getZoom() - 1);
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>

这篇关于如何从gmaps RECTANGLE(叠加)获得4个顶点坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:44