简单地址解析器

问题描述:

我试图从该图“模拟”的代码:简单地址解析器

https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

仅改变Syney到阿雷格里港(形式值和坐标以及)。但是由于地图画布没有显示在屏幕上,所以出现了问题。我检查了代码,没有发现任何错字或语法错误。

我希望有人能帮忙。实际上,我比Google Maps API更多地使用Fusion Tables。

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
<title>GEOCODER</title> 
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
<script src="https://maps.google.com/maps/api/js?sensor=false"></script> 
<script> 

var geocoder; 
var map; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-30.027704, -51,228735); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 

function codeAddress() { 
    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    ); 
    } 
} 
</script> 
</head> 
<body onload="initialize()"> 
<div id="map_canvas" style="width: 320px; height: 480px;"></div> 
<div> 
    <input id="address" type="textbox" value="Porto Alegre, Brasil"> 
    <input type="button" value="Encode" onclick="codeAddress()"> 
</div> 
</body> 
</html>  

您可能交换了两行,导致语法错误。如果你想检查你的JavaScript控制台,你会看到错误信息。如果JS没有达到你期望的目标,那么你应该这样做(或者,对于这一点,几乎总是这样做,只是为了确保没有任何意外的错误)。

如果你有这样的:

 } 
    ); 
    } 
} 
</script> 

你想这样的:

 } 
    } 
); 
} 
</script> 
+0

非常感谢,男人!现在地图工作正常!我认为使用'代码编辑器'就足够了。 JavaScript控制台将是Firebug? – 2012-07-31 01:58:00

+0

Firebug提供访问控制台,是的。但是每个现代浏览器都内置了一个浏览器。确实值得研究如何看待它。这对调试至关重要。 – Trott 2012-07-31 02:55:09

请注意,您在您的标题提供一个相对链接到CSS文件。这适用于谷歌的服务器,但可能不会在你的服务器上,除非你创建了相同的相对路径。试试这个:

<link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
+0

那么,代码已与该链接工作,但我会尝试检查与调试器。谢谢! – 2012-07-31 22:20:27