更新PHP <input>字段与GMaps标记位置

Update PHP <input> field from JavaScript with a GMaps Marker position

本文关键字:GMaps 位置 PHP input 更新 字段      更新时间:2023-09-26

我已经设法添加标记到谷歌地图上点击,但现在我需要更新一个<input>字段内的表单每次新的标记被放置。因此,当放置新标记时,相关的输入将使用标记的纬度和经度进行更新。

我使用的是Google Maps Javascript API v3。

这是我的Javascript:

<script type="text/javascript">
function load() {
    var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(39.47860556892209,-3.328857421875),
        zoom: 7,
        mapTypeId: 'roadmap'
    });
    var infoWindow = new google.maps.InfoWindow;
    google.maps.event.addListener(map, 'click', function(event) {
        //NEITHER OF THESE 2 OPTIONS WORK
        cambiarMarker(event.latLng);
        //document.getElementById("lat").value = event.latLng.lat();
        //document.getElementById("lng").value = levent.latLng.lng();
        document.forms['insertData'].lati.value = location.lat();
        document.forms['insertData'].long.value = location.lng();   
    });
    var markerActual;
    function cambiarMarker(location) {
        //CREATES AND PUT THE NEW MARKER
    }
    downloadUrl("phpsqlajax_genxml2.php", function(data) {
        //GETS INFO FROM AN XML THAT IS CREATED FROM THE DATABASE WITH PHP 
    }
    function bindInfoWindow(marker, map, infoWindow, html) {
        //BINDS INFORMATION WINDOW WITH THE XML INFO
    }

    function downloadUrl(url, callback) {
        //GET THE XML FILE
    }
    function parseXml(str) {
        //PARSES FROM THE XML
    }
    function doNothing() {}
</script>
这是我的HTML:
<body onload="load()">
    Aqui debería salir algo
    <div id="map" style="width: 500px; height: 300px"></div>
    <form name="insertData" action="insertar_datos.php" method="post">
        Nombre: <input type="text" name="name"><br>
        Dirección: <input type="text" name="address"><br>
        Latitud: <input type="text" name="lati" id="lat"><br> <!--HERE'S INPUT1-LATITUDE-->
        Longitud: <input type="text" name="long" id="lng"><br> <!--HERE'S INPUT2-LONG-->
        Teléfono de contacto: <input type="text" name="telefono"><br>
        Comentario: <textarea name="comentario"></textarea><br>
        <input type="submit" value="Enviar">
    </form>
</body>

原则上,您还可以处理输入字段上的change事件并设置标记的坐标。这就是我在申请中所做的。我还处理了标记的drag事件,并在拖动过程中更改坐标。在这种情况下,处理一个标记的dragdragend事件!你必须处理的dragend, drag不是这样,但它很酷,让坐标改变,因为你拖动标记:)

输入字段的更改事件可以这样处理(使用jquery,假设像<input id="coords"...这样的标记):

$('input#coords').change(function () {
    // do the job here: 
    // - convert field coordinates to latLng
    //   for reading the field value use $('input#coords').val()
    // - set the marker position to the new one
});