2017-05-30 11 views
-1

マップをマーカーに合わせてズームして中央に配置します。city_idに基づいて地図の中心を合わせるにはどうすればいいですか?

どのようにして地図をマーカーに合わせてズームすることができますか。表示されるすべてのマーカーはデュバイですので、すべてのマーカーが米国のものであれば地図を中心にしたいですそれは米国に。 これは私のコードだ誰も私を助けてください。あなたがあなたのコード内でinitMap()関数を変更することができgoogle.maps.MapクラスのfitBounds()メソッド

https://developers.google.com/maps/documentation/javascript/reference#Map

を使用することができます

test.phpを

<?php 


    $sql=<<<EOF 
    SELECT * from markers; 
    EOF; 
    $result = $db->query($sql); 
    $yourArray = array(); 
    $index = 0; 

     while($row = $result->fetchArray(SQLITE3_ASSOC)){ 

     $json[] = array(
    'lat' => $row['latitude'], 
    'lon' => $row['longitude'], 
    'name' => $row['name'], 
    'city_id'=>$row['city_id'] 


    ); 

    } 

$db->close(); 


    ?> 

    <!DOCTYPE html> 
    <html> 
    <head> 
     <style> 
     #map { 
     height: 400px; 
     width: 100%; 
      } 
    </style> 
    </head> 
    <body> 
    <h3></h3> 
<div id="map" align="left"></div> 
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDj38Snh4MEIsomOlTZfARryo7V8A_qk9o&libraries=places&callback=initMap"> 
    </script> 
    <?php json_encode($json, JSON_PRETTY_PRINT) ?> 
    <script type="text/javascript"> 
    function initMap() { 
    debugger; 

var locationsJSON = <?php echo json_encode($json, JSON_PRETTY_PRINT) ?>; 

var locations = locationsJSON; 
for (i = 0; i < locations.length; i++) { 

    if(location[i].city_id==1) 
    { 
    var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 4, 
    center: new google.maps.LatLng(31.5546, 74.3572), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 
    } 
    else if(location[i].city_id==2) 
    { 

    var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 4, 
    center: new google.maps.LatLng(25.2048,55.2708), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

    } 
    } 
    var geocoder = new google.maps.Geocoder(); 
var infowindow = new google.maps.InfoWindow(); 


var marker, i; 
for (i = 0; i < locations.length; i++) { 

    console.log(locations[i]); 


    var myLatLng = new google.maps.LatLng(locations[i].lat, locations[i].lon); 

    marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map 
    }); 
    google.maps.event.addListener(marker, 'click', function(evt) { 
    var mark = this; 
    geocoder.geocode({ 
    location: evt.latLng 
    }, function(results, status) { 
    if (status == "OK") { 
     infowindow.setContent(results[0].formatted_address + "<br>" + results[0].geometry.location.toUrlValue(6)); 
     infowindow.open(map, mark); 
    } 
    }); 
    }); 
    }; 
    } 

google.maps.event.addDomListener(window, "load", initMap); 
</script> 
</body> 
</html>  
+0

問題を示す[mcve]を入力してください。 – geocodezip

+0

@geocodezip質問を編集しました。問題の解決に役立ちます –

+0

場所のサンプルデータは表示されません。私はあなたのPHPコードを実行することができません(私は問題を再現する必要があるとは思わない)、問題を示す[mcve]を提供してください(できればSOコードスニペット) – geocodezip

答えて

0

境界を導入してfitBounds()を呼び出します。チェック//Added:

function initMap() { 
    debugger; 

    var locationsJSON = <?php echo json_encode($json, JSON_PRETTY_PRINT) ?>; 

    var locations = locationsJSON; 
    for (i = 0; i < locations.length; i++) { 

     if(location[i].city_id==1) 
     { 
      var map = new google.maps.Map(document.getElementById('map'), 
       { 
        zoom: 4, 
        center: new google.maps.LatLng(31.5546, 74.3572), 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }); 
     } 
     else if(location[i].city_id==2) 
     { 

      var map = new google.maps.Map(document.getElementById('map'), { 
       zoom: 4, 
       center: new google.maps.LatLng(25.2048,55.2708), 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }); 

     } 
    } 
    var geocoder = new google.maps.Geocoder(); 
    var infowindow = new google.maps.InfoWindow(); 

    //Added: create bounds 
    var bounds = new google.maps.LatLngBounds(); 

    var marker, i; 
    for (i = 0; i < locations.length; i++) { 

     console.log(locations[i]); 


     var myLatLng = new google.maps.LatLng(locations[i].lat, locations[i].lon); 

     marker = new google.maps.Marker({ 
      position: myLatLng, 
      map: map 
     }); 

     //Added: extend bounds with new coordinate 
     bounds.extend(myLatLng); 

     google.maps.event.addListener(marker, 'click', function(evt) { 
      var mark = this; 
      geocoder.geocode({ 
       location: evt.latLng 
      }, function(results, status) { 
       if (status == "OK") { 
        infowindow.setContent(results[0].formatted_address + "<br>" + results[0].geometry.location.toUrlValue(6)); 
        infowindow.open(map, mark); 
       } 
       }); 
      }); 
     }; 

     //Added: fit the bounds 
     map.fitBounds(bounds); 
    } 

で始まる私のコメントは、この情報がお役に立てば幸い!

関連する問題