1

我试图在http://www.geocodezip.com/v3_SO_multipleGeocodedMarkersFromXml.html上做这个教程

我已经在我的服务器上包含了 downloadxml.js,但它仍然不起作用。地图加载但没有地图标记。谁能帮忙,因为我不明白为什么它不起作用。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
<script type="text/javascript" src="scripts/downloadxml.js"></script>
<script type="text/javascript">
var map = null;
var geocoder = new google.maps.Geocoder();
var info_window = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
function geocodeAddress(xmldata)
{
    var address = xmldata.getElementsByTagName('address')[0].firstChild.data;
    var city = xmldata.getElementsByTagName('city')[0].firstChild.data;
    var address_google_map = address + ', ' + city + ', ON';
    var info_text = address + '<br />' + city + ' ON';

    geocoder.geocode
    ({'address': address_google_map},
    function (results, status)
    {
      if (status == google.maps.GeocoderStatus.OK) {
        createMarker(results[0].geometry.location, info_text);
      } else { 
        alert("geocode of "+ address +" failed:"+status);
      }
    });
}
 function createMarker(latlng, html)
{
 var marker = new google.maps.Marker
        ({
            map: map, 
            position: latlng
        });
google.maps.event.addListener(marker, 'click', function() {
            info_window.setContent(html);
            info_window.open(map, marker);
        });
bounds.extend(latlng); // Here we tell what are next viewport bounds
}
function initialize ()
{
var myLatLng = new google.maps.LatLng(45.2340684, -75.6287287);
var myOptions =
{
    zoom: 10,
    mapTypeControl: true,
    center: myLatLng,
    zoomControl: true,
    zoomControlOptions:
    {
        style: google.maps.ZoomControlStyle.SMALL
    },
    StreetViewControl: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map'), myOptions);
google.maps.event.addListener
(map, 'click',
function ()
{
    info_window.close();
});

downloadUrl('listings.xml',
function (listings_data)
{
    listings_data = xmlParse(listings_data);
    var markers = listings_data.documentElement.getElementsByTagName('listing');
    var geocoder = new google.maps.Geocoder();
    for (var i = 0; i < markers.length; i++)
    {
        geocodeAddress(markers[i]);
    }
google.maps.event.addListenerOnce(map, 'idle', function() {map.fitBounds(bounds);});
});
}
</script>
</head>
<body onload="initialize();">
<div id="map" style="width: 500px; height: 400px;"></div>

</body>
</html>

和我的 XML

<?xml version="1.0" encoding="UTF-8"?>
<listings>
<listing>
<address>4123 Rideau Valley Rd</address>
<city>MANOTICK</city>
</listing>
<listing>
<address>4456 Rideau Valley Rd</address>
<city>MANOTICK</city>
</listing>
<listing>
<address>111 Bridge St</address>
<city>MANOTICK</city>
</listing>
<listing>
<address>777 Bridge St</address>
<city>Ottawa</city>
</listing>
<listing>
<address>1333 Bridge Street</address>
<city>Manotick</city>
</listing>
</listings>
4

2 回答 2

1

我看到一些问题..您缺少函数的function声明..因此将第 14 行更改为:geocodeAddress(xmldata)

function geocodeAddress(xmldata)
{
... other code continues here ...

然后它应该可以工作,我还注意到var bounds = new google.maps.LatLngBounds();当你添加标记时你没有扩展你的对象,所以记得添加你的createMarker函数来包含它,像这样:

 function createMarker(latlng, html)
{
  var marker = new google.maps.Marker
            ({
                map: map, 
                position: latlng
            });
 google.maps.event.addListener(marker, 'click', function() {
                info_window.setContent(html);
                info_window.open(map, marker);
            });
    bounds.extend(latlng); // Here we tell what are next viewport bounds
}

经过测试,之后一切正常:)干杯!

编辑:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
<script type="text/javascript" src="scripts/downloadxml.js"></script>
<script type="text/javascript">
var map = null;
var geocoder = new google.maps.Geocoder();
var info_window = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
function geocodeAddress(xmldata)
{
    var address = xmldata.getElementsByTagName('address')[0].firstChild.data;
    var city = xmldata.getElementsByTagName('city')[0].firstChild.data;
    var address_google_map = address + ', ' + city + ', ON';
    var info_text = address + '<br />' + city + ' ON';

    geocoder.geocode
    ({'address': address_google_map},
    function (results, status)
    {
      if (status == google.maps.GeocoderStatus.OK) {
        createMarker(results[0].geometry.location, info_text);
      } else { 
        alert("geocode of "+ address +" failed:"+status);
      }
    });
   }
 function createMarker(latlng, html)
{
  var marker = new google.maps.Marker
            ({
                map: map, 
                position: latlng
            });
 google.maps.event.addListener(marker, 'click', function() {
                info_window.setContent(html);
                info_window.open(map, marker);
            });
    bounds.extend(latlng); // Here we tell what are next viewport bounds
}
function initialize ()
{
var myLatLng = new google.maps.LatLng(45.2340684, -75.6287287);
var myOptions =
{
    zoom: 10,
    mapTypeControl: true,
    center: myLatLng,
    zoomControl: true,
    zoomControlOptions:
    {
        style: google.maps.ZoomControlStyle.SMALL
    },
    StreetViewControl: false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
   }
   map = new google.maps.Map(document.getElementById('map'), myOptions);
   google.maps.event.addListener
   (map, 'click',
  function ()
  {
    info_window.close();
  });

  downloadUrl('listings.xml',
  function (listings_data)
  {
    listings_data = xmlParse(listings_data);
    var markers = listings_data.documentElement.getElementsByTagName('listing');
    var geocoder = new google.maps.Geocoder();
    for (var i = 0; i < markers.length; i++)
{
geocodeAddress(markers[i]);
}
google.maps.event.addListenerOnce(map, 'idle', function() {map.fitBounds(bounds);});
});
}
</script>
</head>
<body onload="initialize();">
<div id="map" style="width: 500px; height: 400px;"></div>
</body>
</html>

请记住在您放置 downloadxml.js 的位置创建脚本文件夹,因为它使用路径为:<script type="text/javascript" src="scripts/downloadxml.js"></script>另外不要忘记添加listings.xml

因此,您需要的文件组织为:

index.html
scripts/downloadxml.js
listings.xml
于 2013-03-27T18:03:36.740 回答
0

受 Mauno 解决方案的启发,如果有人提供帮助,我按如下方式解决了这个问题:

function initialize() {
    geocoder = new google.maps.Geocoder();
    info_window = new google.maps.InfoWindow();
    var mapOptions = {
      center: new google.maps.LatLng(40.388397, 0.235348),
      zoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
}

function codeAddress(data){
    var content = 
            '<div class="bocadillo">' +
                '<div class="titulo">'+
                    '<a href="tienda.php?id='+data.id+'">'+data.nombre+'</a>' +
                '</div>'+
                '<div class="content">'+
                    '<span>Ciudad: </span>'+data.ciudad+'<br />'+
                    '<span>Dirección: </span>'+data.direccion+'<br />'+
                    '<span>Teléfono: </span>'+data.telefono+'<br />'+
                    '<span>Email: </span>'+data.mail+'<br />'+
                '</div>'+
            '</div>'
            ;
    var direccion = data.direccion+", "+data.ciudad+", Spain";
    geocoder.geocode( { 'address': direccion}, function(results, status) 
        {
            if(status == google.maps.GeocoderStatus.OK)
            {
                createMarker(results[0].geometry.location, content);
            }
        });
}

function createMarker(latlng, html){
   var image = "img/general/map_icon.png";
   var marker= new google.maps.Marker(
       {
               map: map, 
               position: latlng,
               icon: image
       });
   google.maps.event.addListener(marker, 'click', function() {
        info_window.setContent(html);
        info_window.open(map, marker);
    });
}

然后,给他们打电话:

$(document).ready(function(){
           initialize();
           for(var i=0;i<markers.length;i++){
               codeAddress(markers[i]);
           }
});
于 2014-01-10T16:36:14.847 回答