Regain marker latitude and longitude (Marker)

Asked

Viewed 446 times

3

Follows the code:

function initAutocomplete() {
                debugger;
                var map = new google.maps.Map(document.getElementById('map-create'), {
                    center: { lat: -23.5505199, lng: -46.6333094 },
                    zoom: 11,
                    scrollwheel: false,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                // Create the search box and link it to the UI element.
                var input = document.getElementById('pac-input-create');
                var searchBox = new google.maps.places.SearchBox(input);
                map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

                // Bias the SearchBox results towards current map's viewport.
                map.addListener('bounds_changed', function () {
                    debugger
                    searchBox.setBounds(map.getBounds());
                });

                //Clear Markers
                var markers = [];
                // [START region_getplaces]
                // Listen for the event fired when the user selects a prediction and retrieve
                // more details for that place.
                searchBox.addListener('places_changed', function () {
                    debugger
                    var places = searchBox.getPlaces();

                    if (places.length == 0) {
                        return;
                    }

                    // Clear out the old markers.
                    markers.forEach(function (marker) {
                        marker.setMap(null);
                    });
                    markers = [];

                    // For each place, get the icon, name and location.
                    var bounds = new google.maps.LatLngBounds();
                    places.forEach(function (place) {
                        debugger
                        var icon = {
                            url: place.icon,
                            size: new google.maps.Size(71, 71),
                            origin: new google.maps.Point(0, 0),
                            anchor: new google.maps.Point(17, 34),
                            scaledSize: new google.maps.Size(25, 25)
                        };

                        // Create a marker for each place.
                        markers.push(new google.maps.Marker({
                            map: map,
                            title: place.name,
                            position: place.geometry.location
                        }));

                        if (place.geometry.viewport) {
                            // Only geocodes have viewport.
                            bounds.union(place.geometry.viewport);
                        } else {
                            bounds.extend(place.geometry.location);
                        }
                    });
                    map.fitBounds(bounds);
                });
                // [END region_getplaces]
            }

On the line : markers.push(new google.maps.Marker makes him create bookmark.

Somehow, how do I get lat and long from the marker ?

2 answers

2


You can call getPosition() on the marker, something like that:

var marker = new google.maps.Marker({
                 map: map,
                 title: place.name,
                 position: place.geometry.location
              });
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();
markers.push(marker);
  • I’ve tried with getPosition, please look at image: https://s29.postimg.org/vgyw6s1xj/11_Jan_04_00_05.jpg

  • Man, you saved my day! I’ve been trying for hours.

  • 1

    How nice! Tmj :)

0

I found another way easier and more practical.

Follows the code:

After that line var places = searchBox.getPlaces(); add this to your code:

var lat = places[0].geometry.location.lat();
var long = places[0].geometry.location.lng();

Browser other questions tagged

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