2012-08-25 16 views
6

Tôi coi bản thân mình là một coder PHP trung bình, nhưng tôi hầu như không thể chỉnh sửa một vài mã JavaScript mà tôi cần trên các trang web của mình. Tôi có một địa chỉ trong một biến PHP và tôi muốn một bản đồ đơn giản hiển thị trên trang web của tôi với địa chỉ đó. Tôi đã thực hiện một số nghiên cứu trên internet và tôi tìm thấy rất nhiều mã hóa địa lý, chuyển đổi địa chỉ thành vị trí mà API Google hiểu, nhưng tôi hoàn toàn mất. Không có đoạn mã đơn giản nào để tôi có thể nhúng các biến của mình không?Địa chỉ trong biến PHP, Google Maps nhúng mà không có Javascript?

+0

Kiểm tra câu hỏi này ra , nó sẽ giúp: http://stackoverflow.com/questions/4157750/passing-address-to-google-maps-on-page-load – BenOfTheNorth

Trả lời

21

Bạn có thể sử dụng khung nội tuyến và chuyển địa chỉ dưới dạng tham số URL.

<iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $yourAddress; ?>&output=embed"></iframe> 
+1

Giải pháp hoàn hảo siêu dễ dàng, cảm ơn! – user1555076

+1

Bạn được chào đón! – Napolux

+1

Tôi chỉ sử dụng điều này là tốt, làm việc một điều trị! – Supplement

3

Bạn sẽ phải sử dụng Geocoder API.

Ở đây bạn đi (ban đầu có thể được tìm thấy here):

<head> 
... 
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
<script> 
    var geocoder; 
    var map; 
    function initialize() { 
    geocoder = new google.maps.Geocoder(); 

    var mapOptions = { 
     zoom: 12, //Change the Zoom level to suit your needs 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    //map_canvas is just a <div> on the page with the id="map_canvas" 
    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 

    //Your Variable Containing The Address 
    var address = "<?php echo $AddressVariable; ?>"; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     //places a marker on every 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> 
... 
</head> 

Thêm phần này vào thẻ mở body trang của bạn để khởi tạo bản đồ:

<body onload="initialize()"> 

here's tài liệu cho các Geocoder lớp học.

0
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<?php 
$address = str_replace(" ", "+",$address_variable); 
?> 
<iframe style="width:100%;height:50%;" frameborder="0" id="cusmap" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.it/maps?q=<?php echo $address; ?>&output=embed"></iframe> 
Các vấn đề liên quan