nicogaldo
nicogaldo

Reputation: 585

I need to get the latitude and longitude of my marker

It is a simple map that focuses according to the name of the city, and you can place a marker to indicate where you live. The idea is to save the longitude and latitude in a database, but first I need to get it.

So far I have the code:

var myLatlng = new google.maps.LatLng(37.775, -122.4183333);
    var myOptions =
    {
        zoom: 3,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var pais = 'Argentina'; 
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode( { 'address': pais}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            map.fitBounds(results[0].geometry.bounds);


        } else {
            alert("Geocode was not successful for the following reason: " + status);
        };

        var infoWindow = new google.maps.InfoWindow({
                content: "<div style='font-size: 8pt; font-family: verdana ;' >Mi marca situada en<br></div>"
            });    

        var marker; 
        function placeMarker(location) {
            if ( marker ) {
                marker.setPosition(location);

            } else {
                var clickedLocation = new google.maps.LatLng(location);
                marker = new google.maps.Marker({
                position: location,
                map: map,
                title: 'hola',
                });
            }
        }

        google.maps.event.addListener(map, 'click', function(event) {
            infoWindow.open(map,marker);
            placeMarker(event.latLng);
        });
    });

I have tried with various methods but not get good results. I want to do with gmaps api v3. Here JSFiddle.

Upvotes: 0

Views: 358

Answers (2)

rojo
rojo

Reputation: 24486

What Radu said, mostly. I was updating your fiddle at the same time he was it seems. Additionally, you might find it more convenient to wait until you have that latLng before you construct your infoWindow object, like this:

    var infoWindow = new google.maps.InfoWindow({
        content: "<div class='infoWindow'>Mi marca situada en<br>"
        + "Lat: " + event.latLng.lat() + "<br>"
        + "Lng: " + event.latLng.lng() + "</div>"
    });
    infoWindow.open(map, marker);

If you want to update the contents of your infoWindow, use infoWindow.setContent().

See the Google Maps API reference for an exhaustive list of objects and their properties.

Upvotes: 1

Radu Andrei
Radu Andrei

Reputation: 1073

Uff.. when you place the marker you already have it's position. This position: location contains the position of your marker. In case you somehow need that position you can access it through there or you can do marker.getPosition().

I've updated your fiddle here.

Note: 'k' is lowercase, 'A' is uppercase - in the getPosition() returned object.

Upvotes: 1

Related Questions