简体   繁体   English

在Google数据库中保存Google地图的坐标和数据

[英]Saving coordinates and data from Google maps in a database

I am making a map project where when I choose an area (with google maps drawing tools) an info window pops up and I can write a Name and a Description and then save these and its coordinates. 我正在制作一个地图项目,当我选择一个区域(使用谷歌地图绘图工具)时会弹出一个信息窗口,我可以编写一个名称和一个描述,然后保存这些及其坐标。 I am using a POST form and to this moment I am able to save in my DB the Name and Description but I cant find a way to save the coordinates. 我正在使用POST表单,到目前为止我能够在我的数据库中保存名称和描述,但我找不到保存坐标的方法。 I have already tried with no success to pass it through POST or put some PHP in my JS. 我已经尝试过通过POST或在我的JS中放入一些PHP没有成功。 Here is my js code for the rectangle drawing tool: 这是我的矩形绘图工具的js代码:

google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(rectangle) {

    var ne = rectangle.getBounds().getNorthEast();
    var sw = rectangle.getBounds().getSouthWest();
    var nelat = ne.lat();
    var nelng = ne.lng();
    var swlat = sw.lat();
    var swlng = sw.lng();
    var coordsrec = ';' + nelat.toFixed(6) + ';' + nelng.toFixed(6)+ ';' + swlat.toFixed(6) + ';' + swlng.toFixed(6);
    //console.log(coordsrec);

    contentsr = '<form action="SaveData.php" method="POST"><b>Region Name : </b><br/><input type="text" size="20" name="region_name"/><input type="hidden" name="region_type" value="2"><br/><b>Description : </b><br/><textarea name="region_desc" cols="20" rows="3"></textarea><br/><center><br/><input type="submit" value="Save Region" name="save_region"></center></form>'; 

    var boundsr = new google.maps.LatLng(ne.lat(), ne.lng());

    infoWindow.setContent(contentsr);
    infoWindow.setPosition(boundsr); 
    drawingManager.setDrawingMode(null);
    infoWindow.open(map);
});

I tried sending the coordinates as a hidden field but I can't make it work. 我尝试将坐标作为隐藏字段发送,但我无法使其工作。

I tried it like this: 我试过这样的:

<input type="hidden" name="coords" id="coords" value="coordsrec">

but it saves it as the word "coordsrec" in the DB. 但它将其保存为DB中的“coordsrec”一词。

I also tried: 我也尝试过:

<input type="hidden" name="coords" id="coords" value="<?php echo $coordsrec; ?>"> 

or added the line: 或添加行:

document.getElementById("coords").value = coordsrec; . 

Add the coordinates you create (coordsrec) into a field in the form: 将您创建的坐标(coordsrec)添加到以下形式的字段中:

var coordsrec = nelat.toFixed(6) + ';' + nelng.toFixed(6)+ ';' + swlat.toFixed(6) + ';' + swlng.toFixed(6);

contentsr = '<form action="SaveData.php" method="POST"><b>Region Name : </b><br/>
             <input type="text" size="20" name="region_name"/>
             <input type="hidden" name="region_type" value="2"><br/>
             <b>Description : </b><br/><textarea name="region_desc" cols="20" rows="3"></textarea><br/>
             Coordinates:<br/><input name="coords" type="text" size="40" value="'+coordsrec+'"/><br/>
             <center><br/><input type="submit" value="Save Region" name="save_region"></center>
             </form>'; 

proof of concept fiddle 概念证明小提琴

code snippet: 代码段:

 // This example requires the Drawing library. Include the libraries=drawing // parameter when you first load the API. For example: // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=drawing"> function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: { lat: -34.397, lng: 150.644 }, zoom: 8 }); var infoWindow = new google.maps.InfoWindow(); var drawingManager = new google.maps.drawing.DrawingManager({ drawingMode: google.maps.drawing.OverlayType.RECTANGLE, drawingControl: true, drawingControlOptions: { position: google.maps.ControlPosition.TOP_CENTER, drawingModes: [google.maps.drawing.OverlayType.RECTANGLE] } }); drawingManager.setMap(map); google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(rectangle) { var ne = rectangle.getBounds().getNorthEast(); var sw = rectangle.getBounds().getSouthWest(); var nelat = ne.lat(); var nelng = ne.lng(); var swlat = sw.lat(); var swlng = sw.lng(); var coordsrec = nelat.toFixed(6) + ';' + nelng.toFixed(6) + ';' + swlat.toFixed(6) + ';' + swlng.toFixed(6); //console.log(coordsrec); contentsr = '<form action="SaveData.php" method="POST"><b>Region Name : </b><br/><input type="text" size="20" name="region_name"/><input type="hidden" name="region_type" value="2"><br/><b>Description : </b><br/><textarea name="region_desc" cols="20" rows="3"></textarea><br/>Coordinates:<br/><input name="coords" type="text" size="40" value="' + coordsrec + '"/><br/><center><br/><input type="submit" value="Save Region" name="save_region"></center></form>'; var boundsr = new google.maps.LatLng(ne.lat(), ne.lng()); infoWindow.setContent(contentsr); infoWindow.setPosition(boundsr); drawingManager.setDrawingMode(null); infoWindow.open(map); }); } 
 html, body { height: 100%; margin: 0; padding: 0; } #map { height: 100%; } 
 <div id="map"></div> <!-- Replace the value of the key parameter with your own API key. --> <script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&callback=initMap" async defer></script> 

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

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