Xml 谷歌地图api v3:地理编码多个地址和创建地图标记
我试着做这个教程 我已经在我的服务器上包含了下载的xml.js,但它仍然不起作用。地图已加载,但没有地图标记。有人能帮忙吗?我不明白为什么它不起作用Xml 谷歌地图api v3:地理编码多个地址和创建地图标记,xml,google-maps-api-3,geocoding,Xml,Google Maps Api 3,Geocoding,我试着做这个教程 我已经在我的服务器上包含了下载的xml.js,但它仍然不起作用。地图已加载,但没有地图标记。有人能帮忙吗?我不明白为什么它不起作用 <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>Google Maps Multiple Markers</title
<!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>
谷歌地图多个标记
var-map=null;
var geocoder=new google.maps.geocoder();
var info_window=new google.maps.InfoWindow();
var bounds=new google.maps.LatLngBounds();
函数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+'
'+city+'ON';
地理编码
({'address':address_google_map},
功能(结果、状态)
{
if(status==google.maps.GeocoderStatus.OK){
createMarker(结果[0]。geometry.location,信息文本);
}否则{
警报(“地址+”的地理代码失败:+状态);
}
});
}
函数createMarker(latlng,html)
{
var marker=new google.maps.marker
({
地图:地图,
职位:latlng
});
google.maps.event.addListener(标记'click',函数(){
设置内容(html);
信息窗口。打开(地图、标记);
});
bounds.extend(latlng);//这里我们告诉您下一个视口边界是什么
}
函数初始化()
{
var mylatng=new google.maps.LatLng(45.2340684,-75.6287287);
变异性肌肽=
{
缩放:10,
mapTypeControl:true,
中心:myLatLng,
动物控制:对,
ZoomControl选项:
{
样式:google.maps.ZoomControlStyle.SMALL
},
街景控制:错误,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById('map'),myOptions);
google.maps.event.addListener
(映射“单击”,
函数()
{
信息窗口。关闭();
});
下载URL('listings.xml',
功能(列表和数据)
{
listings\u data=xmlParse(listings\u data);
var markers=listings_data.documentElement.getElementsByTagName('listing');
var geocoder=new google.maps.geocoder();
对于(var i=0;i
还有我的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>
里多谷路4123号
马诺迪克
里多谷路4456号
马诺迪克
桥街111号
马诺迪克
桥街777号
渥太华
桥街1333号
马诺蒂克
我看不出什么问题。。您缺少geocodeAddress(xmldata)
函数的声明。。因此,将第14行更改为:
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>
我看没有什么问题。。您缺少geocodeAddress(xmldata)
函数的声明。。因此,将第14行更改为:
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>
受Mauno solution的启发,我解决了以下问题,如果有人帮忙的话:
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);
});
}
函数初始化(){
geocoder=新的google.maps.geocoder();
info_window=new google.maps.InfoWindow();
变量映射选项={
中心:新的google.maps.LatLng(40.388397,0.235348),
缩放:6,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map_canvas”),mapOptions);
}
函数代码地址(数据){
变量内容=
'' +
''+
'' +
''+
''+
“Ciudad:”+data.Ciudad+“
”+
“Dirección:”+data.direccion+“
”+
'Teléfono:'+data.telefono+'
'+
'电子邮件:'+data.mail+'
'+
''+
''
;
var direccion=data.direccion+,“+data.ciudad+”,西班牙”;
geocoder.geocode({'address':direccion},函数(结果,状态)
{
if(status==google.maps.GeocoderStatus.OK)
{
createMarker(结果[0]。几何体。位置,内容);
}
});
}
函数createMarker(latlng,html){
var image=“img/general/map\u icon.png”;
var marker=new google.maps.marker(
{
地图:地图,
位置:latlng,
图标:图像
});
google.maps.event.addListener(标记'click',函数(){
设置内容(html);
信息窗口。打开(地图、标记);
});
}
然后,给他们打电话:
$(document).ready(function(){
initialize();
for(var i=0;i<markers.length;i++){
codeAddress(markers[i]);
}
});
$(文档).ready(函数(){
初始化();
对于(var i=0;i,受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);
});
}
函数初始化(){
geocoder=新的google.maps.geocoder();
info_window=new google.maps.InfoWindow();
变量映射选项={
中心:新的google.maps.LatLng(40.388397,0.235348),
缩放:6,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map_canvas”),mapOptions);
}
函数代码地址(数据){
变量内容=
'' +
''+
'' +
''+
''+
“Ciudad:”+data.Ciudad+“
”+
“Dirección:”+data.direccion+“
”+
'Teléfono:'+data.telefono+'
'+
“电子邮件:”+data.mail+