我想要显示基于区域名称的谷歌地图。我正在我的jsp页面中获取区域名称(即中国湖,加利福尼亚州-美国)。我需要通过它,并显示谷歌地图。如何通过Google javascript API做到这一点?
提前感谢
发布于 2011-03-19 13:45:37
您是否尝试过将区域名称作为位置传递给地理编码器的函数getLocations?
让我们知道这是否有效。
编辑:对不起,请稍等。我会张贴一些清单。
function userLocSearch(location){
userInput = location;
if (GBrowserIsCompatible()) {
geocoder = getGeocoder();
geocoder.getLocations(location, function(responce){
if(responce.Status.code==200){
if(responce.Placemark.length==1){
//just one result
mapInit(responce.Placemark[0].Point.coordinates[1],responce.Placemark[0].Point.coordinates[0],14);
}else{
//more than one result
mapInit(responce.Placemark[0].Point.coordinates[0],responce.Placemark[0].Point.coordinates[1],14);
updateInfoText(responce.Placemark.length);
}
}else{
//error
//no result
geocoder.getLocations("USA", function(responce){
mapInit(responce.Placemark[0].Point.coordinates[1],responce.Placemark[0].Point.coordinates[0],6);
});
}
});
}else{
//TODO
//browser not compartible
}
return false;
}mapInit是我的另一个函数。初始化映射的那个。我粘贴的那个只是为了通过搜索字符串获取所需的地理坐标数据
发布于 2011-03-20 03:20:26
我找到了解决方案。下面是我的代码:
<script type="text/javascript">
var map = null;
var geocoder = null;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setUIToDefault();
geocoder = new GClientGeocoder();
showAddress("Melapalayam,Tirunelveli,Tamilnadu");
}
}
function showAddress(address) {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 15);
var marker = new GMarker(point, {draggable: true});
map.addOverlay(marker);
GEvent.addListener(marker, "click", function() {
alert(address);
});
}
}
);
}
}
window.onload = initialize;
</script>https://stackoverflow.com/questions/5360358
复制相似问题