<!DOCTYPE html> <html> <head> <style type="text/css"> html, body, #map-canvas { height: 100%; margin: 0; padding: 0;} </style> <script type="text/javascript" src="http://www.google.cn/maps/api/js"> </script> <script type="text/javascript"> var geocoder; var map; var infowindow = new google.maps.InfoWindow(); var marker; function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(40.730885,-73.997383); var mapOptions = { zoom: 8, center: latlng, mapTypeId: 'roadmap' } map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); //codeAddress(); } function codeAddress() { //var address = document.getElementById("address").value; var address = "上海市南京西路1618號6樓"; geocoder.geocode( { 'address': address}, function(results, status) { console.log(status); console.log(results); console.log(google.maps.GeocoderStatus); 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); } }); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <button onclick="codeAddress()">Click</button> <div id="map-canvas"></div> </body> </html>
调用Google "MAP"接口是一定使用www.google.cn,因为http://www.google.cn/maps/中,我无法找到是如何调用API的方法和参数.在codeAddress()方法,总是返回"ZERO_RESULTS"
解决了