How do I pick up the location of a marker and store it in the database using google map?

Asked

Viewed 895 times

1

I have a marker on my map, it is "draggable", I would like to know how I can find the latitude and longitude of this marker, and later store in the database.

I read the google map api documentation but couldn’t find how to do it.

@Edit:

  var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 15,
          center: franca
        });

        function CarregarEnderecoPorLatLng(latitude, longitude){
          var latlong = [latitude, longitude];
          var marker5 = new google.maps.Marker({
           position: new google.maps.LatLng(latitude, longitude),
           map: map
       });
        return latlong;
        }

        map.addListener('click', function (e) {
          CarregarEnderecoPorLatLng(e.latLng.lat(), e.latLng.lng());
        })

In this attempt to click he adds a marker, I thought of returning an array with latitude and longitude, but I’m confused on how I’m going to get the return of that latitude and longitude through that function.

  • Let me get this straight. You by means of a draggable marker go walking by google maps and position in such place, pick up these coordinates, put in the bank so that when giving refresh, it appears "Static" in that coordinate?

  • Exactly, I also tried to click on the map to add a marker, and I thought this function return the latitude and longitude it received through an array, and consume in php this array to make the Insert, I will give an Edit in what I tried...

  • Got it, check it out, I have a google maps project that has a function similar to what you want. Through clicks on the map, it dynamically sends the latitude and longitude to an input field, so from this input field I copy and paste in a window.open and register the marker, after registration it gives an auto refresh in index.php and voilá, the marker is in the exact location. If it fits, I can answer with that my function.

  • I believe you can help yes, send me to try ;)

1 answer

0


Leticia, follows the function in which you do what you want, you just have to position it somewhere in your javascript where your initMap

First, position two inputs somewhere you want, follow below my:

<label for="latitude">Latitude:</label>
<input id="latMap"/>

<label for="longitude">Longitude:</label>
<input id="lngMap"/>

So in your <script> or file .js from your map, somewhere inside your map initMap you put this:

// Função de pegar as coordenadas com o clique no mapa
google.maps.event.addListener(map, 'click', function(event) {
                    document.getElementById('latMap').value = event.latLng.lat();
                    document.getElementById('lngMap').value = event.latLng.lng();
                });

            function mapDivClicked (event) {
                var target = document.getElementById('map'),
                    posx = event.pageX - target.offsetLeft,
                    posy = event.pageY - target.offsetTop,
                    bounds = map.getBounds(),
                    neLatlng = bounds.getNorthEast(),
                    swLatlng = bounds.getSouthWest(),
                    startLat = neLatlng.latitude(),
                    endLng = neLatlng.longitude(),
                    endLat = swLatlng.latitude(),
                    startLng = swLatlng.longitude();

                document.getElementById('posX').value = posx;
                document.getElementById('posY').value = posy;
                document.getElementById('latitude').value = startLat + ((posy/350) * (endLat - startLat));
                document.getElementById('longitude').value = startLng + ((posx/500) * (endLng - startLng));
            };

And then, don’t forget to reference this in your google maps api link, I’ll put mine and you can notice that there are some references using the &, you add so &libraries=geometry&callback=initMap

But I’ll put mine as it is for you to see, just put your API key there and put it on your index.

<script src="https://maps.googleapis.com/maps/api/js?key=SUA_CHAVE_API&v=3&libraries=geometry&callback=initMap" defer > </script>

If you have any questions, answer me below that we will work together to align everything.

  • latitude and longitude appeared, but I’m having trouble passing this variable to a php variable, I tried: var latitude = startLat + ((Posy/350) * (endLat - startLat); var longitude = startLng + ((posx/500) * (endLng - startLng)); <? php $latitude = "<script>Document.write(latitude)</script>"; echo "$latitude"; ? > In the console returns that latitude is not defined

  • In this case, as it is a field that will receive a value after the mouse click, it starts as null. I believe that’s why the latitude variable is undefined. But what’s your goal? You want to take the values of these two inputs to make a registration?

  • My idea is to store the latitude and longitude of the two fields in the comic

  • Just a question, you already have this registration system and process to show the data of the comic in the ready map?

  • I did it with a form: <form action="" method="post"> <label for="latitude">Latitude:</label> <input id="latMap" name="latitude"/> <label for="longitude">Longitude:</label> <input="lngMap" name="longitude"/> <input type="Submit"> </form> <?php $latitude = $_POST["latitude"]; $longitude = $_POST["longitude"]; echo "$latitude"; ? > I believe I can proceed from now on, otherwise I open a new topic with the new question, thank you very much.

  • I don’t have it yet, I’m just thinking the main logic, I believe I can now

  • Got it, look, I can give you a link to the video of the people who helped me make this communication google maps with mysqli, if you want, I can pass you.

  • Pass me please, any new study material is welcome

  • Here Leticia, this link is the first where I have detailed How to use Google Maps with PHP and Mysqli: https://www.youtube.com/watch?v=2ET5E0G3vK4 And this is part 2 of how to register it: https://www.youtube.com/watch?v=auWR9KzzRM

  • I’m gonna watch!!!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.