-1

I'm using the following javascript in order to populate a address input field when I click on the map. I also get the latitude and longitude values inside hidden fields from the below javascript.

And now I would like to create a cookie with the latitude and longitude values. Any ideas on how to implement this?

var map;
var geocoder;
var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP };

function initialize() {
    var myOptions = {
        center: new google.maps.LatLng(38.89103282648849, -97.646484375),
        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    geocoder = new google.maps.Geocoder();
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    google.maps.event.addListener(map, 'click', function(event) {
        placeMarker(event.latLng);
    });

    var marker;
    function placeMarker(location) {
        if(marker){ //on vérifie si le marqueur existe
            marker.setPosition(location); //on change sa position
        }else{
            marker = new google.maps.Marker({ //on créé le marqueur
                position: location,
                map: map
            });
        }
        document.getElementById('lat').value=location.lat();
        document.getElementById('lng').value=location.lng();
        getAddress(location);
    }

    function getAddress(latLng) {
        geocoder.geocode( {'latLng': latLng},
            function(results, status) {
                if(status = google.maps.GeocoderStatus.OK) {
                    if(results[0]) {
                        document.getElementById("address").value = results[0].formatted_address;
                    }
                    else {
                        document.getElementById("address").value = "No results";
                    }
                }
                else {
                    document.getElementById("address").value = status;
                }
            });
    }
}
google.maps.event.addDomListener(window, 'load', initialize);
Theopap
  • 715
  • 1
  • 10
  • 33
  • Possible duplicate of [How do I create and read a value from cookie?](https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – Joshua Terrill Jan 06 '18 at 09:08

1 Answers1

1

With js-cookie you can read and manipulate cookies as simple as possible .

Just include js-cookie file inside your page

<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>

and use it where you want

// insert or update cookie
Cookies.set("KEY", "VALUE");

// get specific cookie
var value = Cookies.get("KEY");
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
  • thanks for the reply @Farnabaz, I'm kind of a newbee with javascript, so could please give more info on how and where I can add this to my javascript? – Theopap Jan 06 '18 at 08:56
  • @Theopap updated, all you need is to include file in your page – Farnabaz Jan 06 '18 at 09:05
  • Thanks for the help and info @Farnabaz!!! One more question though, where and how exactly do I put the second script u provided? – Theopap Jan 06 '18 at 09:21
  • @Theopap If you want to save lat&long of selected marker, place `Cookie.set` inside `placeMarker` function. `Cookie.set('lat.long', location.lat() + "," + location.lng()` – Farnabaz Jan 06 '18 at 09:33