2012-08-25 1 views
6

私は自分自身を平均的なPHPコーダーと考えていますが、自分のウェブサイトに必要な数のJavaScriptコードをほとんど編集することはできません。私はPHP変数のアドレスを持っており、そのアドレスを持つ私のWebページに表示される単純なマップが欲しいと思います。私はインターネット上でいくつかの調査をしましたが、Google APIが理解できる場所に住所を変換することで、多くの情報ジオコーディングが見つかりましたが、私は完全に失われています。変数を埋め込むことのできる単純なコードはありませんか?PHP変数のアドレス、Javascriptを使用しないGoogleマップの埋め込みですか?

+0

アウトこの質問をチェック、それは助けてくれるはずです:http://stackoverflow.com/questions/4157750/passing-address-to-google-maps-on-page-load – BenOfTheNorth

答えて

21

iframeを使用して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

パーフェクトen超簡単なソリューション、ありがとう! – user1555076

+1

あなたは大歓迎です! – Napolux

+1

私はこれも使いました。 – Supplement

3

Geocoder APIを使用する必要があります。

ここでは、(元は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> 

マップを初期化するために、あなたのページの開口部bodyタグにこれを追加します。

<body onload="initialize()"> 

そしてhere'sのドキュメントGeocoderクラス。

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> 
関連する問題