2017-06-17 10 views
1

これまでに何度も尋ねられてきたことは知っていますが、私が見てきた答えは他にはありません。基本的に、マップのコンテナは(ページ上に500x300ピクセルの空白として)表示されますが、マップはありません。私が何をしても、私は地図を表示することができません。地図が表示されていないGoogleマップのコンテナ

私はおそらく推測しているように、私はGoogleマップに慣れていないので、どんな助けもありがとうございます。ズームを設定し、「オーバーフロー:可視」を追加しました。これは、とりわけ、別の提案です。

うまくいけば、このフィドル作品:https://jsfiddle.net/7cvj565j/

findafitter.php - 地図を

<div class="container text-center"> 
    <div class="row"> 
     <div class="col-sm-8"> 
      <div id="map-container"> 
       <div id="googleMap" style="height:300px; width:500px;"> 
        <script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&callback=initMap" async defer></script> 
        <script src="includes/findafitter2.js"></script> 
       </div> 
      </div> 
     </div> 
     <div class="col-sm-4"> 
      Enter your postcode to find your nearest fitter. Do you have any questions? <a href="contact.php">Let us know!</a> 
      <input id="address" type="textbox" value=""> 
      <input type="button" value="Find My Nearest Fitter" onclick="checkZip(getElementById('address').value)"> 
     </div> 
    </div> 
</div> 

findafitter2.jsを表示されている - 地図

var geocoder; 
var map; 
var markers = []; 

function initialize() 
{ 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(40.768505, -111.853244); 
    var myOptions = 
    { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("googleMap"), myOptions); 
    addPostCode('84101'); 
    addPostCode('84102'); 
    addPostCode('84103'); 
    addPostCode('84104'); 
    addPostCode('84105'); 
} 

function addPostCode(zip) 
{ 
    geocoder.geocode({ 'address': zip}, 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, 
       name: zip 
      }); 
      markers.push(marker); 
     } 
     else 
     { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 

function checkZip(zip) 
{ 
    var distance = Number.MAX_VALUE; 
    var index = 0; 
    geocoder.geocode({ 'address': zip}, function(results, status) 
    { 
     if (status == google.maps.GeocoderStatus.OK) 
     { 
      for(ix=0; ix< markers.length; ix++) 
      { 
       var tmp = getDistance(results[0].geometry.location, markers[ix].position); 
       if (tmp < distance) 
       { 
        distance = tmp; 
        index = ix; 
       } 
      } 
      alert('nearest zipcode is :' + markers[index].name); 
     } 
    }); 
} 

function getDistance(latlng1, latlng2) 
{ 
    var R = 6371; // Radius of the earth in km 
    var dLat = (latlng2.lat()-latlng1.lat()) * Math.PI/180; // Javascript functions in radians 
    var dLon = (latlng2.lng()-latlng1.lng()) * Math.PI/180; 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
      Math.cos(latlng1.lat() * Math.PI/180) * Math.cos(latlng2.lat() * Math.PI/180) * 
      Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c; // Distance in km 
    d = d * 0.621371192; // convert to miles 
    return d; 
} 

ためのコードstyles.css - これは私がやって、そのコードが動作することがありますどのように非常に似ている - 該当ビット

#map-container 
{ 
    min-height: 300px; 
    color: #000000; 
    overflow: visible; 
} 

Googleマップコードは、SO、私のマップコードは、それを破ったかどうかを確認するために(Google maps - Postcode plotting)を投稿し、他から取りました多くの人々のために。

うまくいけば十分です。前もって感謝します!

+0

代わりにGoogleマップの自動生成iframeを使用してみましたか? –

答えて

2

のiframe選択が最も簡単ですが、私はあなたのJSのバージョンが必要と思いました。 https://jsfiddle.net/scorpio777/af2nzqh1/

APIキーが正しくないか、使用しているdivやスタイリングについて何かが原因で機能していない可能性があります。 マップをウェブページに配置する最も簡単な方法は、Googleマップ上の場所を検索し、必要に応じてサイズをカスタマイズし、埋め込みコードをiframeとして取得することです。

1. Search the wanted location on Google maps 
2. Choose "Share or embed map" from the top-right "hamburger" menu 
3. Choose "Embed map" tab > "Custom size" 
and you have the iframe code. 
+0

あなたのjavascriptソリューションはうまくいきました。ありがとう!私は、特定の郵便番号に最も近いマーカーを見つけることができる必要があります - これを行うにはiframeメソッドが影響しますか? (私の質問のコードはうまくいけばこれを既に行います) – lukecolli98

2
  1. Googleマップで必要な場所を検索します。
  2. 「共有」をクリックし
  3. 「埋め込みマップ」
  4. グーグルマップ上をクリックして、あなたがこの方法を考えるものを教えてください

を使用するためのiframeを自動生成します!

Googleマップのコードを別のStackOverflowの投稿から取り込んで動作させるよりも良いと思われます。

<iframe src="https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d2965.0824050173574!2d-93.63905729999999!3d41.998507000000004!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1sWebFilings%2C+University+Boulevard%2C+Ames%2C+IA!5e0!3m2!1sen!2sus!4v1390839289319" width="100%" height="300px" frameborder="0" style="border:0"></iframe>

+0

私は、ユーザーが指定した郵便番号に最も近いマーカーを見つけることができる必要があります。うまくいけば、私が提供したコードはそれを行いますが、地図が表示されないので、私はそれをテストすることができませんでした。私はiframeソリューションが好きですが、私にこれをさせることができますか? – lukecolli98

関連する問題