JavaScript Google Maps API V3
我正在尝试使用Google Maps API v3中的geocode
将地址转换为纬度/经度,但它无效。
其余代码工作正常。
我确信这是一个非常简单的问题!!
代码
var map;
function initialize() {
var infoWindow = new google.maps.InfoWindow;
var myLatLng = new google.maps.LatLng(-23.000516, -43.413473);
var myOptions = {
zoom: 15,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('mapa'), myOptions);
// Download do XML
downloadUrl("http://podium.pvidesign.com.br/mapa/casas.xml", function(data) {
var icone = 'http://podium.pvidesign.com.br/mapa/imagens/casa.png';
var imoveis = data.documentElement.getElementsByTagName("imovel");
for (var i = 0; i < imoveis.length; i++) {
//var latlng = new google.maps.LatLng(parseFloat(imoveis[i].getAttribute("lat")), parseFloat(imoveis[i].getAttribute("lng")));
var logradouro = imoveis[i].getAttribute("logradouro");
var endereco = imoveis[i].getAttribute("endereco");
var numero = imoveis[i].getAttribute("numero");
var bairro = imoveis[i].getAttribute("bairro");
var cidade = imoveis[i].getAttribute("cidade");
var imagem = imoveis[i].getAttribute("img");
var end_completo = logradouro + " " + endereco + ", " + numero + ", " + bairro + ", " + cidade;
var nome = imoveis[i].getAttribute("nome");
var desc = imoveis[i].getAttribute("desc");
var html = "<b>" + nome + "</b><br>" + "<img src='" + imagem + "'><br>" + desc + "<br>" + end_completo;
// Coletando Lat e Long atraves do endereço
var geocoder = new google.maps.Geocoder();
geocoder.geocode( {'address': end_completo}, function(results, status) {
var latLong = results[0].geometry.location;
}); // Fim Coletando
var houseMarker = new google.maps.Marker({
title:nome,
position:latLong,
map:map,
icon:icone
});
bindInfoWindow(houseMarker, map, infoWindow, html);
}
});
// Balão de informação
function bindInfoWindow(houseMarker, map, infoWindow, html) {
google.maps.event.addListener(houseMarker, 'mouseover', function() {
infoWindow.setContent(html);
infoWindow.open(map, houseMarker);
});
google.maps.event.addListener(houseMarker, 'mouseout', function() {
infoWindow.close();
});
};
};
答案 0 :(得分:2)
从葡萄牙语翻译(疯狂猜测)看起来您正在尝试对包含以下内容的地址进行地理编码:街道,地址,号码,社区和城市。它可能太多了,我会尝试只是街道,数字,城市,看看是否有帮助。
此外,您的代码非常乐观,没有检查响应中的状态代码。您应该检查response
中的Status Codes,如果不是google.maps.GeocoderStatus.OK
,那么回复中就不会有任何结果。
尝试使用不同样本输入的Google Maps API v3 Geocoder Tool(来自您的数据)查看其行为方式,您可能会发现格式化地址,数据本身等方面的问题。尝试让您的地址看起来像你用什么来运送包裹。
另请注意,您的方法只能在加载地图时对地址进行地理编码。如果您需要显示更多内容,则可能需要使用服务器端地理编码。请查看Geocoding Strategies文章,并确保为您的用例使用正确的服务。