2016-11-06 54 views
-1

我爲我的網站使用yii2-google-maps-markers。 它工作正常。 我想創建一個搜索引擎,所以我想在顯示搜索結果後更改google map的中心值。 我可以更改中心位置,但所有制造商都被刪除。 如何通過使用指定地址的js而不需要刪除製造商來改變中心。 我的代碼如下:通過使用yii2-google-maps-markers設置Google地圖中心

view.php

<?php 
    echo GoogleMaps::widget([ 
     'userLocations' => $locat, 
     'googleMapsUrlOptions' => [ 
      'key' => Yii::$app->params['GOOGLE_API_KEY'], 
     ], 
     'googleMapsOptions' => [ 
     ], 
     'wrapperHeight' => '350px', 
    ]); 
?> 

和當前的解決方案

<script type="text/javascript"> 

// Run function after page loaded 
document.addEventListener('DOMContentLoaded', function() { 
    show_map_theo_address("some where, America"); 
}, false); 

    // This is the minimum zoom level that we'll allow 
    function show_map_theo_address(address) { 
     var geocoder, vitri; 
     var minZoomLevel = 15; 
     geocoder = new google.maps.Geocoder(); 
     if (geocoder) { 
      geocoder.geocode({ 
       'address': address 
      }, function (results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        if (status != google.maps.GeocoderStatus.ZERO_RESULTS) { 
         vitri = results[0].geometry.location; 
         var map = new google.maps.Map(document.getElementById('map_canvas'), { 
          zoom: minZoomLevel, 
          center: new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()), 
          mapTypeId: google.maps.MapTypeId.ROADMAP 
         }); 
         } 
        } 
       } 
      }); 
     } 

    } 

但它不工作。 請幫忙。

+0

意味着什麼不行..你有錯誤(在瀏覽器控制檯檢查)或其他? – scaisEdge

+0

沒有錯誤,但所有制造商都被刪除。我不想要這個。 – Sam

回答

1

您應該分開的邏輯。對於查看地圖第一部分和所有標記爲獲得新的標記位置和設置中心

該怎麼一回事,因爲你創建(重建)的第二部分的地圖,當你用實際show_map_theo_address功能

爲避免標記刪除您應該

創建一個全局變量地圖

<script> 
    var map; 
    ...... 

移動地圖的創建功能show_map_theo_address()

var map = new google.maps.Map(document.getElementById('map_canvas'), { 
     zoom: minZoomLevel, 
     center: new google.maps.LatLng(YourInitialCenterLat, YourInitialCenterLng), 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }); 

功能show_map_theo_address()內使用setCenter外

// This is the minimum zoom level that we'll allow 
function show_map_theo_address(address) { 
    var geocoder, vitri; 
    var minZoomLevel = 15; 
    geocoder = new google.maps.Geocoder(); 
    if (geocoder) { 
     geocoder.geocode({ 
      'address': address 
     }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       if (status != google.maps.GeocoderStatus.ZERO_RESULTS) { 
        vitri = results[0].geometry.location; 
         map.setCenter(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng())); 
        } 
       } 
      } 
     }); 
    } 

} 
+0

謝謝scaisEdge。我使用yii2小部件,所以我認爲我不能像第一部分那樣做全局初始化。第一部分已經通過「echo GoogleMaps :: widget ...」完成。如果我錯了,請教我。 – Sam

+0

我不知道谷歌地圖Yii2 Widgte ..我直接使用..谷歌地圖在JavaScript(對我來說更ieasy)..我已經建議的邏輯..你應該適應你需要..當嘗試不要在你的功能重新創建地圖..希望這是有用的 – scaisEdge

+0

謝謝你。我會再次嘗試通過js創建gg映射。 – Sam

相關問題