Reputation: 11
i have a magento site where my products are businesses. I want to add a google map to the product page and need someone to help me with my code.
I can call the zip code by typing
<?php echo $_product->getpostcode() ?>
I have taken code from the internet and am trying to put it together for my site. here is the code:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
var geocoder;
var map;
var address = '<?php echo $_product->getpostcode() ?>'
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
}
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>
<body onload="initialize()">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
At the moment it is using the lat long in the code. How can i change it to using the zip code?`
Upvotes: 0
Views: 1385
Reputation: 777
//i have working function use it.
function initialize() {
var address = "<?php echo $address?>";
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(50.317408,11.12915);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("location-canvas"), myOptions);
if (geocoder) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var infowindow = new google.maps.InfoWindow(
{ content: '<b>'+address+'</b>',
size: new google.maps.Size(150,50)
});
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title:address
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
} else {
alert("Address Not found");
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
}
Upvotes: 0
Reputation: 12973
Maps always use co-ordinates. That how maps work. If you want to use a zipcode, you need to convert that to co-ordinates — that process is called geocoding.
You have a geocoding function in your code, which won't work at the moment because you don't have an element called address
, but it can be used with a little adjustment.
You currently have a global variable called address
, which contains your zipcode, so get your geocoder function to use it:
At the bottom of function initialize()
, add a call to the geocoder function, passing it the global variable:
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
codeAddress(address);
}
Change that function to accept a parameter:
function codeAddress(addy) {
and then to use it:
// var address = document.getElementById('address').value;
// The above line needs to be removed; the one below amended
geocoder.geocode( { 'address': addy}, function(results, status) {
Upvotes: 1