使用Google Maps Geocoder标记地址

时间:2013-10-10 13:09:38

标签: google-maps google-maps-api-3 google-maps-markers

问题:如何更改我的代码,以便我可以使用普通的人工地址,并在地图上将其标记为“。”

好的 - 我一直在研究这个问题,我似乎无法做到这一点。我有以下代码在地图上显示自定义标记 - 100%正确工作:

function LoadMap() {
    var locations = new Array(
        [34.01843,-118.491046], [34.006673,-118.486562], [34.009714,-118.480296]
    );

    var markers = new Array();

    var mapOptions = {
        center: new google.maps.LatLng(34.019000, -118.455458),
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scrollwheel: false
    };

    var map = new google.maps.Map(document.getElementById('map'), mapOptions);

    $.each(locations, function(index, location) {
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(location[0], location[1]),
            map: map
        });

        var myOptions = {
            content: '<div class="infobox"><div class="image"><img src="http://html.realia.byaviators.com/assets/img/tmp/property-tiny-1.png" alt=""></div><div class="title"><a href="detail.html">1041 Fife Ave</a></div><div class="area"><span class="key">Area:</span><span class="value">200m<sup>2</sup></span></div><div class="price">€450 000.00</div><div class="link"><a href="detail.html">View more</a></div></div>',
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(-146, -190),
            zIndex: null,
            closeBoxURL: "",
            infoBoxClearance: new google.maps.Size(1, 1),
            position: new google.maps.LatLng(location[0], location[1]),
            isHidden: false,
            pane: "floatPane",
            enableEventPropagation: false
        };
        marker.infobox = new InfoBox(myOptions);
        marker.infobox.isOpen = false;

        var myOptions = {
            draggable: true,
            content: '<div class="marker"><div class="marker-inner"></div></div>',
            disableAutoPan: true,
            pixelOffset: new google.maps.Size(-21, -58),
            position: new google.maps.LatLng(location[0], location[1]),
            closeBoxURL: "",
            isHidden: false,
            enableEventPropagation: true
        };
        marker.marker = new InfoBox(myOptions);
        marker.marker.open(map, marker);
        markers.push(marker);

        google.maps.event.addListener(marker, "click", function (e) {
            var curMarker = this;

            $.each(markers, function (index, marker) {
                // if marker is not the clicked marker, close the marker
                if (marker !== curMarker) {
                    marker.infobox.close();
                    marker.infobox.isOpen = false;
                }
            });

            if(curMarker.infobox.isOpen === false) {
                curMarker.infobox.open(map, this);
                curMarker.infobox.isOpen = true;
                map.panTo(curMarker.getPosition());
            } else {
                curMarker.infobox.close();
                curMarker.infobox.isOpen = false;
            }

        });
    });
}

我想要做的是将“var locations”从lat / long更改为json'human'地址 - 从中​​:

var locations = new Array(
        [34.01843,-118.491046], [34.006673,-118.486562], [34.009714,-118.480296]
    );

这样的事情:

var locations = JSON.parse('[{"address":"New Street, Salisbury, UK","content":"hello world from salisbury","status":"live"},{"address":"86000 Poitiers, France","content":"hello world from poitiers"},{"address":"66000 Perpignam, France","content":"hello world from perpignam"}]');

显然,在google.maps.Marker position中使用这些地址之前,我需要以某种方式对这些地址进行地理编码 - 但我似乎无法弄清楚如何。

我尝试将标记代码更改为:

    geocoder.geocode( {'address': '1 George St Sydney NSW 2000 Australia' },
              function(results, status) {

                if (status == google.maps.GeocoderStatus.OK) {

                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()),
                        map: map,
                        icon: 'transparent.png'
                    });
                } 
                else {
                  document.getElementById("text_status").value = status;
                }
              }
            );

但没有渲染标记:(

非常感谢任何帮助/建议。

0 个答案:

没有答案