I want to display the google Map based on zone name. I am getting Zone name in my jsp page (ie. China Lake, California - USA). 开发者_开发知识库I need to pass that and display google Map. How to do that through Google javascript API?
Thanks in advance
I found the solution. here is my code:
<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>
- Gnaniyar Zubair
Have you tried to pass the zonename as location to the geoCoder's function getLocations ?
Let us know if this has worked.
EDIT: Sorry 1 moment hold on. I will post some listing.
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 is here an other of my functions. The one which initializes the map. The one I pasted is just for getting the required geocooredinates by a search string
精彩评论