簡體   English   中英

我需要獲取標記的緯度和經度

[英]I need to get the latitude and longitude of my marker

這是一張簡單的地圖,根據城市名稱進行聚焦,您可以放置​​一個標記來指示您的居住地。 這個想法是將經度和緯度保存在數據庫中,但是首先我需要獲取它。

到目前為止,我有代碼:

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);
        });
    });

我嘗試了各種方法,但未獲得良好的結果。 我想使用gmaps api v3。 這里是JSFiddle

Uff ..當您放置標記時,您已經擁有了它的位置。 position: location包含標記的位置。 如果您以某種方式需要該位置,則可以從那里訪問它,也可以執行marker.getPosition()

我在這里更新了你的小提琴

注意:“ k”是小寫字母,“ A”是大寫字母-在getPosition()返回的對象中。

拉杜大部分是怎么說的。 好像他在同一時間更新的小提琴 此外,在構造infoWindow對象之前,等到擁有latLng可能會更方便,如下所示:

    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);

如果要更新infoWindow的內容,請使用infoWindow.setContent()

有關對象及其屬性的詳盡列表,請參見Google Maps API參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM