-1

我需要一种方法来使用以前保存的数据刷新和重新加载我的 Google 地图和标记。本质上,我希望用户能够自动完成地址,存储来自查询的信息的 lat、long 和 place_id,然后如果他们再次返回页面,则使用最新的搜索标记和地图图像重新加载地图.

 $('#jobLoadingAddressLat').val(place.geometry.location.lat())
 $('#jobLoadingAddressLng').val(place.geometry.location.lng())
 $("#jobLoadingAddressID").val(place.place_id);

这就是我用来解析地图信息的方法。当用户返回页面时,我是否可以使用此信息重新加载地图和标记?

将信息存储在 cookie 中是最佳选择吗?大家有什么推荐的?提前致谢。

4

1 回答 1

0

我在 html 中使用这个脚本

        <script>
            var geocoder;
            var map;
            var marker;
            var latLng;

            function codeAddress() {

                // alert("hizo click ....!!!!")
                // We obtain the address and assign it to a variable
                if (document.getElementById("fPersonal:inputDir").value.length > 0) {
                    // alert("Direccion mayor a 0");
                    var combo;
                    var selectValue;
                    var indice;
                    indice = document
                            .getElementById("fPersonal:cComuna_input").selectedIndex;

                    //Validate if combo is selected
                    if (indice == null || indice == 0 || indice == -1) {

                        selectValue = '';
                        alert("Seleccione una Comuna para busqueda : ");

                    } else {
                        combo = document
                                .getElementById('fPersonal:cComuna_input');
                        selectValue = combo.options[combo.selectedIndex].text;

                        var address = document
                                .getElementById("fPersonal:inputDir").value
                                + ", " + selectValue + "," + " Chile";
                        //alert(address);

                        // We create the Geocoder Object
                        var geocoder = new google.maps.Geocoder();
                        // We make the request indicating the address and invoke the function
                        // geocodeResult sending all the result obtained
                        geocoder.geocode({
                            'address' : address
                        }, geocodeResult);
                    }
                }
            }

            function geocodeResult(results, status) {

                if (results.length == 1) {
                    // Verificamos el estatus
                    if (status == 'OK') {

                        // If there are found results, we center and repaint the map                            
                        // this to remove any pin before put
                        var mapOptions = {
                            zoom : 15,
                            center : results[0].geometry.location,
                            mapTypeId : google.maps.MapTypeId.ROADMAP
                        };
                        map = new google.maps.Map(document
                                .getElementById("fPersonal:map"),
                                mapOptions);

                        // I get latitude and longitude in text fields
                        document.getElementById("fPersonal:inputLatitud").value = results[0].geometry.location
                                .lat();
                        document.getElementById("fPersonal:inputLongitud").value = results[0].geometry.location
                                .lng();

                        // fitBounds will zoom in on the map with the right zoom according to what you are looking for
                        map.fitBounds(results[0].geometry.viewport);

                        // We draw a marker with the location of the first result obtained
                        var markerOptions = {
                            position : results[0].geometry.location
                        }
                        marker = new google.maps.Marker(markerOptions);
                        marker.setMap(map);

                        google.maps.event.addListener(map, 'click',
                                function(event) {
                                    //alert(5);
                                    updateMarker(event.latLng);
                                });

                    } else {

                        // If there are no results or an error has occurred
                        // we launched a message with the error
                        //alert("Geocoding no tuvo éxito debido a: " + status);

                        document.getElementById("fPersonal:inputLatitud").value = '';
                        document.getElementById("fPersonal:inputLongitud").value = '';
                        document.getElementById("fPersonal:inputDir").value = '';

                    }
                } else {

                    // If there are no results or an error has occurred
                    // we launched a message with the error
                    // alert ("results is greater than 1:" + results.length);

                    document.getElementById("fPersonal:inputLatitud").value = '';
                    document.getElementById("fPersonal:inputLongitud").value = '';
                    document.getElementById("fPersonal:inputDir").value = '';

                }
            }

            // I update the position on the scoreboard
            function updateMarker(location) {
                marker.setPosition(location);
                updateMarkerPosition(location);

            }
            // I retrieve latitude, longitude and direction to put it in the form
            function updateMarkerPosition(latLng) {

                //alert(latLng.lat());
                document.getElementById("fPersonal:inputLatitud").value = latLng
                        .lat();
                document.getElementById("fPersonal:inputLongitud").value = latLng
                        .lng();
            }
        </script>

我希望能帮上忙

于 2018-07-11T20:46:33.260 回答