I'm trying to currently show the location of an address when page loads, but my map doesn't show at all. I tried to alert(address) and it works. It shows the correct address I'm trying to get. But I'm not sure if I'm doing this right?
<script>
var geocoder;
var map;
function codeAddress() {
geocoder = new google.maps.Geocoder();
//var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var city_state_zip = document.getElementById("city_state_zip").innerHTML;
var street_address = document.getElementById("street_address").innerHTML;
var address = street_address + " " + city_state_zip;//document.getElementById(street_addres +" "+ city_state_zip).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);
}
});
}
window.onload = function(){
codeAddress();
}
</script>
I bined the initialize() with the codeAddress() found in this example: .html#GeocodingStatusCodes The reason I want to bine this because I dont want to load the map with a random LatLng, and I want it to load with a address I have already.
Does anyone have any idea if I'm even doing this correctly?
Thanks!
I'm trying to currently show the location of an address when page loads, but my map doesn't show at all. I tried to alert(address) and it works. It shows the correct address I'm trying to get. But I'm not sure if I'm doing this right?
<script>
var geocoder;
var map;
function codeAddress() {
geocoder = new google.maps.Geocoder();
//var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var city_state_zip = document.getElementById("city_state_zip").innerHTML;
var street_address = document.getElementById("street_address").innerHTML;
var address = street_address + " " + city_state_zip;//document.getElementById(street_addres +" "+ city_state_zip).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);
}
});
}
window.onload = function(){
codeAddress();
}
</script>
I bined the initialize() with the codeAddress() found in this example: http://code.google./apis/maps/documentation/javascript/geocoding.html#GeocodingStatusCodes The reason I want to bine this because I dont want to load the map with a random LatLng, and I want it to load with a address I have already.
Does anyone have any idea if I'm even doing this correctly?
Thanks!
Share Improve this question edited Jan 6, 2012 at 16:10 hellomello asked Jan 6, 2012 at 4:37 hellomellohellomello 8,59742 gold badges154 silver badges310 bronze badges 4- Since you have the latlng mented out Im not sure if that will cause a problem. Also, were you able to display the map without all the geocoding? – asawilliams Commented Jan 6, 2012 at 4:58
- when I unment the latlng, it goes to that specific latlng, then it goes straight to the address desired, how do i not allow it to go to the latlng and just go straight to address onload? – hellomello Commented Jan 6, 2012 at 5:20
- 1 do all the geocoder stuff before you set the map options and use results[0].geometry.location to populate the center property of the options – asawilliams Commented Jan 6, 2012 at 5:39
- I think that was one of my problems, I'm not sure how to set up the geocode to LatLng, so it can populate based on my address – hellomello Commented Jan 6, 2012 at 6:05
1 Answer
Reset to default 4The following modification of the script makes it work for those who was in the same boat as me. This now allows me to load a specific address when the page is loaded, it will automatically detect the lat,lng from my html.
This is a slight modification from the google's website.
<script>
var geocoder;
var map;
function codeAddress() {
geocoder = new google.maps.Geocoder();
var lat='';
var lng=''
var city_state_zip = document.getElementById("city_state_zip").innerHTML;
var street_address = document.getElementById("street_address").innerHTML;
var address = street_address + " " + city_state_zip;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
lat = results[0].geometry.location.lat(); //getting the lat
lng = results[0].geometry.location.lng(); //getting the lng
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);
}
});
var latlng = new google.maps.LatLng(lat, lng);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
window.onload = function(){
codeAddress();
}
</script>