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?
Trả lời
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>
Giải pháp hoàn hảo siêu dễ dàng, cảm ơn! – user1555076
Bạn được chào đón! – Napolux
Tôi chỉ sử dụng điều này là tốt, làm việc một điều trị! – Supplement
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()">
Và here's tài liệu cho các Geocoder
lớp học.
Các liên kết này sử dụng đầy đủ để you..not vào liên kết chính xác nhưng việc sử dụng nó đầy đủ :) tc ..
1) remove-address-bubble-from-google-maps-iframe
2) google maps
<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>
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