javascript - 我需要获取标记的纬度和经度

标签 javascript google-maps google-maps-api-3

这是一个简单的 map ,根据城市名称进行聚焦,您可以放置​​标记来指示您居住的地方。这个想法是将经度和纬度保存在数据库中,但首先我需要获取它。

到目前为止我有代码:

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 .

最佳答案

拉杜所说的,主要是。我是updating your fiddle同时他似乎也是。此外,您可能会发现在构建 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 reference获取对象及其属性的详尽列表。

关于javascript - 我需要获取标记的纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876806/

相关文章:

php - MySQL 和 PHP 与 Google map - 数据源仅在本地工作,无法从 http url 访问时工作

javascript - 使用字符串地址而不是 latlng 立即加载 gmap

google-maps-api-3 - 从API获取建筑尺寸

javascript - 在信息窗口中嵌入街景图片 - Googlemaps v3

javascript - 如何从数组中获取javascript中的列表

c# - 我如何发送文件以接收返回值(适用于 Internet Explorer 8-9)?

javascript - 谷歌分析如何检测用户离开页面?

android - android google maps中两点之间的距离/时间

jquery - 使用markerclusterer v3获取谷歌地图范围内的标记列表

javascript - 了解 javascript 函数范围