2012-07-30 41 views
0

我有一个magento网站,我的产品是企业。 我想添加一个谷歌地图到产品页面,需要有人来帮助我使用我的代码。取得属性(邮政编码),并在Google地图API中使用它3

我可以通过键入

<?php echo $_product->getpostcode() ?> 

我从网上取码,我试图把它一起为我的网站调用邮政编码。这里是代码:

<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> 

目前它正在使用中的代码纬度长。我怎样才能改变它使用的邮政编码?`

回答

1

地图总是使用坐标。地图如何工作。如果您想使用邮政编码,则需要将其转换为协调—,该过程称为地理编码

您的代码中有地理编码功能,目前无法使用,因为您没有名为address的元素,但可以稍作调整使用。

您目前有一个名为address全局变量,其中包含你的邮政编码,以便让你的地理编码器功能,使用它:

在功能initialize()底部,添加一个调用地理编码器功能,通过它的全球变量:

map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 
codeAddress(address); 
} 

更改其功能是接受一个参数:

function codeAddress(addy) { 

然后吨Ø使用它:

// var address = document.getElementById('address').value; 
// The above line needs to be removed; the one below amended 
geocoder.geocode({ 'address': addy}, function(results, status) { 
+0

嗨安德鲁,我怎么能地址添加到全局变量?目前我可以通过输入以下命令获得邮政编码<?php echo $ _product-> getpostcode()?>我按照你的指示,在地图显示悉尼时。我认为这是从我的代码中的长和经度数字 – user1564305 2012-07-31 13:06:14

+0

Works for me:http://jsfiddle.net/acleach/73Pf6/(使用伦敦邮政编码而不是您的任何PHP输出)。地理编码将正常工作并重新设置地图,否则它将输出失败消息并显示“mapOptions”位置,这确实是悉尼地区。 – 2012-07-31 13:12:46

+0

嗨安德鲁,看起来像我到达那里。当我手动输入邮政编码时,它也适用于我。正如我之前提到的,我将从php echo命令中获取邮政编码。我现在需要把它放到var地址中。当我尝试这样做时,由于以下原因,我得到Geocode不成功Zero_results – user1564305 2012-07-31 13:33:48

0
//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); 
    } 
    }); 
} 

}

相关问题