2016-11-19 8 views
-1

Googleマップには20個のマーカーがあります。ポップアップウィンドウがあります。 ここでは、すべてのマーカーの詳細をリストとして(1つずつ)キャプチャしたいと考えています。どうすればこのようにすることができますか? 私は私が考える私の全体のコードのHTML + Javascriptを以下のマーカーデータをGoogle Mapsのリストアイテムとして入手してください。JavaScriptコード

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <meta charset="UTF-8"> 
 
     <title>Search Near by location</title> 
 
     <script type="text/javascript" src="js/jquery.js"></script> 
 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
 
     <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script> 
 
    <!-- Latest compiled and minified CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 

 
    <!-- Latest compiled and minified JavaScript --> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
 
    
 
     <style type="text/css"> 
 
      #map { 
 
       height: 400px; 
 
       width: 600px; 
 
       border: 1px solid #333; 
 
       margin-top: 0.6em; 
 
      } 
 
    
 
    
 
    
 
     </style> 
 

 
    <script type="text/javascript"> 
 
      $(function(){ 
 
    //get the checked nearby place 
 
       $('.case').click(function(){ 
 
        $(':checkbox').attr('checked',false); 
 
        $('#'+$(this).attr('id')).attr('checked',true); 
 
        search_types(map.getCenter()); 
 
       }); 
 
       
 
      }); 
 
      
 
     var map; 
 
      var infowindow; 
 
      var markersArray = []; 
 
      var pyrmont = new google.maps.LatLng(20.268455824834792, 85.84099235520011); 
 
      var marker; 
 
      var geocoder = new google.maps.Geocoder(); 
 
      var infowindow = new google.maps.InfoWindow(); 
 
    
 
    
 
      // var waypoints = [];     
 
      function initialize() { 
 
       map = new google.maps.Map(document.getElementById('map'), { 
 
        mapTypeId: google.maps.MapTypeId.ROADMAP, 
 
        center: pyrmont, 
 
        zoom: 14 
 
       }); 
 
       infowindow = new google.maps.InfoWindow(); 
 
       //document.getElementById('directionsPanel').innerHTML=''; 
 
       search_types(); 
 
       } 
 

 
      function createMarker(place,icon) { 
 
       var placeLoc = place.geometry.location; 
 
       var marker = new google.maps.Marker({ 
 
        map: map, 
 
        position: place.geometry.location, 
 
        icon: icon, 
 
        visible:true 
 
        
 
       }); 
 
    
 
       markersArray.push(marker); 
 
       google.maps.event.addListener(marker, 'click', function() { 
 
        infowindow.setContent("<b>Name:</b>"+place.name+"<br><b>Address:</b>"+place.vicinity+"<br><b>Reference:</b>"+place.reference+"<br><b>Rating:</b>"+place.rating+"<br><b>Id:</b>"+place.id); 
 
        infowindow.open(map, this); 
 
     
 
      //get the clicked ATM name 
 
      document.getElementById("demo").innerHTML=place.name; 
 
         // get the ATM address   
 
     document.getElementById("demo1").innerHTML=place.vicinity; 
 
       
 
     
 
       }); 
 
       
 
       
 
    } 
 
    
 
     
 
      var source=""; 
 
      var dest=''; 
 
      
 
      function search_types(latLng){ 
 
       clearOverlays(); 
 
       
 
       if(!latLng){ 
 
        var latLng = pyrmont; 
 
       } 
 
       var type = $('.case:checked').val(); 
 
       var icon = "https://cdn2.iconfinder.com/data/icons/banking-7/24/banking-atm-1-48.png"; 
 
       
 
    
 
       var request = { 
 
        location: latLng, 
 
        radius: 2500, 
 
        types: [type] //e.g. school, restaurant,bank,bar,city_hall,gym,night_club,park,zoo 
 
       }; 
 
       
 
       var service = new google.maps.places.PlacesService(map); 
 
       service.search(request, function(results, status) { 
 
        map.setZoom(14); 
 
        if (status == google.maps.places.PlacesServiceStatus.OK) { 
 
         for (var i = 0; i < results.length; i++) { 
 
          results[i].html_attributions=''; 
 
          createMarker(results[i],icon); 
 
     
 
     
 
         } 
 
     
 
     
 
        } 
 
     
 
       }); 
 
       
 
      } 
 
      
 
      
 
      // Deletes all markers in the array by removing references to them 
 
      function clearOverlays() { 
 
       if (markersArray) { 
 
        for (i in markersArray) { 
 
         markersArray[i].setVisible(false) 
 
        } 
 
        //markersArray.length = 0; 
 
     //document.getElementById("demo3").innerHTML = markersArray.length;  
 
       } 
 
      } 
 
      google.maps.event.addDomListener(window, 'load', initialize); 
 
      
 
      function clearMarkers(){ 
 
       $('#show_btn').show(); 
 
       $('#hide_btn').hide(); 
 
       clearOverlays() 
 
      } 
 
      function showMarkers(){ 
 
       $('#show_btn').hide(); 
 
       $('#hide_btn').show(); 
 
       if (markersArray) { 
 
        for (i in markersArray) { 
 
         markersArray[i].setVisible(true) 
 
        } 
 
         
 
       } 
 
      } 
 
      
 
      function showMap(){ 
 
       
 
       var imageUrl = 'http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png'; 
 
       var markerImage = new google.maps.MarkerImage(imageUrl,new google.maps.Size(24, 32)); 
 
       var input_addr=$('#address').val(); 
 
     geocoder.geocode({address: input_addr}, function(results, status) { 
 
        if (status == google.maps.GeocoderStatus.OK) { 
 
         var latitude = results[0].geometry.location.lat(); 
 
         var longitude = results[0].geometry.location.lng(); 
 
         var latlng = new google.maps.LatLng(latitude, longitude); 
 
         if (results[0]) { 
 
          map.setZoom(14); 
 
          map.setCenter(latlng); 
 
          marker = new google.maps.Marker({ 
 
           position: latlng, 
 
           map: map, 
 
           icon: markerImage, 
 
           draggable: true 
 
           
 
          }); 
 
          $('#btn').hide(); 
 
          $('#latitude,#longitude').show(); 
 
          $('#address').val(results[0].formatted_address); 
 
          $('#latitude').val(marker.getPosition().lat()); 
 
          $('#longitude').val(marker.getPosition().lng()); 
 
          infowindow.setContent(results[0].formatted_address); 
 
          infowindow.open(map, marker); 
 
          search_types(marker.getPosition()); 
 
          google.maps.event.addListener(marker, 'click', function() { 
 
           infowindow.open(map,marker); 
 
           
 
          }); 
 
         
 
         
 
          google.maps.event.addListener(marker, 'dragend', function() { 
 
           
 
           geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) { 
 
            if (status == google.maps.GeocoderStatus.OK) { 
 
             if (results[0]) { 
 
              $('#btn').hide(); 
 
              $('#latitude,#longitude').show(); 
 
              $('#address').val(results[0].formatted_address); 
 
              $('#latitude').val(marker.getPosition().lat()); 
 
              $('#longitude').val(marker.getPosition().lng()); 
 
      
 
      
 
      
 
             } 
 
             
 
             infowindow.setContent(results[0].formatted_address); 
 
             var centralLatLng = marker.getPosition(); 
 
             search_types(centralLatLng); 
 
             infowindow.open(map, marker); 
 
            } 
 
           }); 
 
     
 
        
 
     
 
          }); 
 
            
 
         
 
         } else { 
 
          alert("No results found"); 
 
         } 
 
        } else { 
 
         alert("Geocoder failed due to: " + status); 
 
        } 
 
       }); 
 
       
 
      } 
 
            
 

 
$(function(){ 
 
    
 
    // add multiple select/deselect functionality 
 
    $("#selectall").click(function() { 
 
      $('.case').attr('checked', this.checked); 
 
    }); 
 
    
 
    // if all checkbox are selected, check the selectall checkbox 
 
    // and viceversa 
 
    $(".case").click(function(){ 
 
    
 
     if($(".case").length == $(".case:checked").length) { 
 
      $("#selectall").attr("checked", "checked"); 
 
     } else { 
 
      $("#selectall").removeAttr("checked"); 
 
     } 
 
    
 
    }); 
 
}); 
 
</SCRIPT> 
 
    
 
    </head> 
 
    <body> 
 

 

 
     <label>Address: </label><input id="address" type="text" style="width:400px;" value=""/> 
 
     
 
    <label class="btn btn-primary"> 
 
    <input type="checkbox" name="mytype" class="case btn btn-primary" id="btn" value="atm" onClick="showMap();">ATM 
 
    </label> 
 
     <br/> 
 
     <div id="map"></div> 
 
     <input type="text" id="latitude" style="display:none;" placeholder="Latitude"/> 
 
     <input type="text" id="longitude" style="display:none;" placeholder="Longitude"/> 
 
     <!-- <input type="button" id="hide_btn" value="hide markers" onClick="clearMarkers();" />--> 
 
     <input type="button" id="show_btn" value="atm" onClick="showMarkers();" style="display:none;" /> 
 

 
     <div id="test"></div> 
 
    
 
    <p id="demo"></p> 
 
    <p id="demo1"></p> 
 
    <p id="demo3"></p> 
 
    
 

 

 
    </body> 
 

 
</html>

+1

ループmarkerArrayトラフ?? –

+0

@Jonasw私が試したが、あなたが私が \t \t \t \t \t \t \tを試してみましたが、このような何か –

+0

を提案してくださいすることができ、最後のマーカーの詳細を返します(VAR I = 0;私は

答えて

1

1つのオプションは、あなたが(createMarker機能で)マーカーを作成するよう適切にスコープの変数にマップの下に表示するデータを取り込むことであろうマーカーを作成するループは、それを終了し、ループの最後で、それを表示したいのdivにそれを置く:

if (status == google.maps.places.PlacesServiceStatus.OK) { 
    htmlStr = "<table border='1'><tbody>"; 
    for (var i = 0; i < results.length; i++) { 
    results[i].html_attributions = ''; 
    createMarker(results[i], icon); 
    } 
    document.getElementById("demo").innerHTML = htmlStr + "</tbody></table>"; 
} 

コードスニップペット:

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Search Near by location</title> 
 

 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places"></script> 
 
    <!-- Latest compiled and minified CSS --> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> 
 

 
    <!-- Latest compiled and minified JavaScript --> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> 
 

 
    <style type="text/css"> 
 
    #map { 
 
     height: 400px; 
 
     width: 600px; 
 
     border: 1px solid #333; 
 
     margin-top: 0.6em; 
 
    } 
 
    </style> 
 

 
    <script type="text/javascript"> 
 
    $(function() { 
 
     //get the checked nearby place 
 
     $('.case').click(function() { 
 
     $(':checkbox').attr('checked', false); 
 
     $('#' + $(this).attr('id')).attr('checked', true); 
 
     search_types(map.getCenter()); 
 
     }); 
 
    }); 
 
    var map; 
 
    var htmlStr = ''; 
 
    var infowindow; 
 
    var markersArray = []; 
 
    var pyrmont = new google.maps.LatLng(20.268455824834792, 85.84099235520011); 
 
    var marker; 
 
    var geocoder = new google.maps.Geocoder(); 
 
    var infowindow = new google.maps.InfoWindow(); 
 

 
    function initialize() { 
 
     map = new google.maps.Map(document.getElementById('map'), { 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
 
     center: pyrmont, 
 
     zoom: 14 
 
     }); 
 
     infowindow = new google.maps.InfoWindow(); 
 
     search_types(); 
 
    } 
 

 
    function createMarker(place, icon) { 
 
     var placeLoc = place.geometry.location; 
 
     var marker = new google.maps.Marker({ 
 
     map: map, 
 
     position: place.geometry.location, 
 
     icon: icon, 
 
     visible: true 
 
     }); 
 
     markersArray.push(marker); 
 
     htmlStr += "<tr><td><b>Name:</b>" + place.name + "<br><b>Address:</b>" + place.vicinity + "<br><b>Reference:</b>" + place.reference + "<br><b>Rating:</b>" + place.rating + "<br><b>Id:</b>" + place.id + "</td></tr>"; 
 
     google.maps.event.addListener(marker, 'click', function() { 
 
     infowindow.setContent("<b>Name:</b>" + place.name + "<br><b>Address:</b>" + place.vicinity + "<br><b>Reference:</b>" + place.reference + "<br><b>Rating:</b>" + place.rating + "<br><b>Id:</b>" + place.id); 
 
     infowindow.open(map, this); 
 
     }); 
 
    } 
 
    var source = ""; 
 
    var dest = ''; 
 

 
    function search_types(latLng) { 
 
     clearOverlays(); 
 
     if (!latLng) { 
 
      var latLng = pyrmont; 
 
     } 
 
     var type = $('.case:checked').val(); 
 
     var icon = "https://cdn2.iconfinder.com/data/icons/banking-7/24/banking-atm-1-48.png"; 
 
     var request = { 
 
      location: latLng, 
 
      radius: 2500, 
 
      types: [type] //e.g. school, restaurant,bank,bar,city_hall,gym,night_club,park,zoo 
 
     }; 
 
     var service = new google.maps.places.PlacesService(map); 
 
     service.search(request, function(results, status) { 
 
      map.setZoom(14); 
 
      if (status == google.maps.places.PlacesServiceStatus.OK) { 
 
      htmlStr = "<table border='1'><tbody>"; 
 
      for (var i = 0; i < results.length; i++) { 
 
       results[i].html_attributions = ''; 
 
       createMarker(results[i], icon); 
 
      } 
 
      document.getElementById("demo").innerHTML = htmlStr + "</tbody></table>"; 
 
      } 
 
     }); 
 
     } 
 
     // Deletes all markers in the array by removing references to them 
 

 
    function clearOverlays() { 
 
     if (markersArray) { 
 
     for (i in markersArray) { 
 
      markersArray[i].setVisible(false) 
 
     } 
 
     } 
 
    } 
 
    google.maps.event.addDomListener(window, 'load', initialize); 
 

 
    function clearMarkers() { 
 
     $('#show_btn').show(); 
 
     $('#hide_btn').hide(); 
 
     clearOverlays() 
 
    } 
 

 
    function showMarkers() { 
 
     $('#show_btn').hide(); 
 
     $('#hide_btn').show(); 
 
     if (markersArray) { 
 
     for (i in markersArray) { 
 
      markersArray[i].setVisible(true) 
 
     } 
 

 
     } 
 
    } 
 

 
    function showMap() { 
 
     var imageUrl = 'http://chart.apis.google.com/chart?cht=mm&chs=24x32&chco=FFFFFF,008CFF,000000&ext=.png'; 
 
     var markerImage = new google.maps.MarkerImage(imageUrl, new google.maps.Size(24, 32)); 
 
     var input_addr = $('#address').val(); 
 
     geocoder.geocode({ 
 
     address: input_addr 
 
     }, function(results, status) { 
 
     if (status == google.maps.GeocoderStatus.OK) { 
 
      var latitude = results[0].geometry.location.lat(); 
 
      var longitude = results[0].geometry.location.lng(); 
 
      var latlng = new google.maps.LatLng(latitude, longitude); 
 
      if (results[0]) { 
 
      map.setZoom(14); 
 
      map.setCenter(latlng); 
 
      marker = new google.maps.Marker({ 
 
       position: latlng, 
 
       map: map, 
 
       icon: markerImage, 
 
       draggable: true 
 
      }); 
 
      $('#btn').hide(); 
 
      $('#latitude,#longitude').show(); 
 
      $('#address').val(results[0].formatted_address); 
 
      $('#latitude').val(marker.getPosition().lat()); 
 
      $('#longitude').val(marker.getPosition().lng()); 
 
      infowindow.setContent(results[0].formatted_address); 
 
      infowindow.open(map, marker); 
 
      search_types(marker.getPosition()); 
 
      google.maps.event.addListener(marker, 'click', function() { 
 
       infowindow.open(map, marker); 
 
      }); 
 
      google.maps.event.addListener(marker, 'dragend', function() { 
 
       geocoder.geocode({ 
 
       'latLng': marker.getPosition() 
 
       }, function(results, status) { 
 
       if (status == google.maps.GeocoderStatus.OK) { 
 
        if (results[0]) { 
 
        $('#btn').hide(); 
 
        $('#latitude,#longitude').show(); 
 
        $('#address').val(results[0].formatted_address); 
 
        $('#latitude').val(marker.getPosition().lat()); 
 
        $('#longitude').val(marker.getPosition().lng()); 
 
        } 
 
        infowindow.setContent(results[0].formatted_address); 
 
        var centralLatLng = marker.getPosition(); 
 
        search_types(centralLatLng); 
 
        infowindow.open(map, marker); 
 
       } 
 
       }); 
 
      }); 
 
      } else { 
 
      alert("No results found"); 
 
      } 
 
     } else { 
 
      alert("Geocoder failed due to: " + status); 
 
     } 
 
     }); 
 
    } 
 
    $(function() { 
 
     // add multiple select/deselect functionality 
 
     $("#selectall").click(function() { 
 
     $('.case').attr('checked', this.checked); 
 
     }); 
 
     // if all checkbox are selected, check the selectall checkbox 
 
     // and viceversa 
 
     $(".case").click(function() { 
 
     if ($(".case").length == $(".case:checked").length) { 
 
      $("#selectall").attr("checked", "checked"); 
 
     } else { 
 
      $("#selectall").removeAttr("checked"); 
 
     } 
 
     }); 
 
    }); 
 
    </SCRIPT> 
 
</head> 
 

 
<body> 
 
    <label>Address:</label> 
 
    <input id="address" type="text" style="width:400px;" value="" /> 
 
    <label class="btn btn-primary"> 
 
    <input type="checkbox" name="mytype" class="case btn btn-primary" id="btn" value="atm" onClick="showMap();">ATM 
 
    </label> 
 
    <br/> 
 
    <div id="map"></div> 
 
    <input type="text" id="latitude" style="display:none;" placeholder="Latitude" /> 
 
    <input type="text" id="longitude" style="display:none;" placeholder="Longitude" /> 
 
    <!-- <input type="button" id="hide_btn" value="hide markers" onClick="clearMarkers();" />--> 
 
    <input type="button" id="show_btn" value="atm" onClick="showMarkers();" style="display:none;" /> 
 
    <div id="test"></div> 
 
    <p id="demo"></p> 
 
    <p id="demo1"></p> 
 
    <p id="demo3"></p> 
 
</body> 
 

 
</html>

+0

恐ろしいタクあなたは遅刻の返事をおかけして... –

0

を貼り付けている、MarkersArrayはあなたが必要なすべてが含まれています。それをループさせます。

htmlStr += "<tr><td><b>Name:</b>" + place.name + "<br><b>Address:</b>" + place.vicinity + "<br><b>Reference:</b>" + place.reference + "<br><b>Rating:</b>" + place.rating + "<br><b>Id:</b>" + place.id + "</td></tr>"; 

前に変数を初期化します。

for(i=0;i<MarkersArray;i++){ 
    marker=MarkersArray[i]; 
    container=document.body;//change to your element 
    //create a new paragraph, and add it to the marker (if you need to change it later) 
marker.html=document.createElement("p"); 
    //add the name and vicinity to the paragraph 
marker.html.innerHTML=marker.name+":"+marker.vicinity; 
    //put it on the site (into the container) 
container.appendChild(marker.html); 
} 
+0

質問の回答をするたびに、 OPのコードの何が間違っているのか、そしてそれをどのように並べ替えるのか。 –

関連する問題