0

如何将地理编码与已知坐标相结合?可以说,我想显示坐标数组中的标记。

function codeAddress()
{
    var latlng = new google.maps.LatLng(42.745334, 12.738430);
    var address = document.getElementById("location").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            var mapOptions = {
            zoom: 11,
            center: results[0].geometry.location,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }

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

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: image_icon
            });

        }           

    });

}

所以我有一个值的纬度坐标和地址,我如何将这两者结合到地图中?

谢谢!

4

1 回答 1

0
function codeAddress()
{

    // The following sets up your array.  Most likely you will retrieve the data 
    // from elsewhere, so this is just meant as an illustration.
    var coords = [];        
    coords.push({lat:100,long:200, caption:'yo'});
    coords.push({lat:150,long:250, caption:'yo yo'});
    coords.push({lat:200,long:250, caption:'yo ho ho'});

    var address = document.getElementById("location").value;

    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            var mapOptions = {
            zoom: 11,
            center: results[0].geometry.location,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }

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

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: image_icon
            });


            var myLatlng;

            // loop through all the elements in the array, and add markers for each to the map object you've already created.
            for (i=0;i<coords.length;i++){ 
                myLatlng = new google.maps.LatLng(coords[i].lat,coords[i].lng);
                marker = new google.maps.Marker({
                    position: myLatlng,
                    map: map,
                    title:coords[i].caption
                });
            }                


        }    


    });

}

​</p>

于 2012-11-08T06:31:09.090 回答