2014-06-13 75 views
0

我有兩個javascript函數。 1)InitializeGoogleMap和2)GetPlaces調用Javascript函數Google Maps

當頁面加載時第一個加載地圖,第二個函數根據按鈕事件點擊時使用eval暴露的db的值加載標記。

兩個功能正常工作的問題是,GetPlaces功能再次加載地圖加載標記,而不是我想要的GetPlaces功能通過使用創建的同一地圖InitializeGoogleMap

腳本:

 <script language="javascript" type="text/javascript"> 

     function InitializeGoogleMap() { 

      try { 
       DirectionsDisplay = new google.maps.DirectionsRenderer(); 
       var LatitudeLongitude = new google.maps.LatLng(19.1969813, 72.9962491); 
       var GoogleMapOptions = 
       { 
        zoom: 10, 
        center: LatitudeLongitude, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 
       var map = new google.maps.Map($("#me")[0], GoogleMapOptions); 

       DirectionsDisplay.setMap(map); 
       DirectionsDisplay.setPanel($("#DivDirectionRouteStatus")[0]); 



      } 
      catch (E) { 
       alert(E.message); 
      } 



     } 




function GetPlaces() { 

     try{ 


      var markers = 
       [<asp:Repeater ID="rptMarkers" runat="server"> 
          <ItemTemplate> 
             { 
              "title": '<%# Eval("Name") %>', 
              "lat": '<%# Eval("Latitude") %>', 
              "lng": '<%# Eval("Longatude") %>', 
              "description": '<%# Eval("Description") %>' 
             } 
          </ItemTemplate> 
          <SeparatorTemplate> 
           , 
          </SeparatorTemplate> 
          </asp:Repeater> 
      ]; 

      DirectionsDisplay = new google.maps.DirectionsRenderer(); 


      var mapOptions = { 
       center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
       zoom: 8, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      var map = new google.maps.Map($("#me")[0], mapOptions); 

      for (i = 0; i < markers.length; i++) { 
       var data = markers[i] 
       var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
       var marker = new google.maps.Marker({ 
        position: myLatlng, 
        map: map, 
        title: data.title 
       }); 
       (function (marker, data) { 
        google.maps.event.addListener(marker, "click", function (e) { 
         infoWindow.setContent(data.description); 
         infoWindow.open(map, marker); 
        }); 
       })(marker, data); 
      } 


     } 
     catch (E) 
     { 
      alert(E.message); 
     } 

     } 
    </script> 
+0

爲什麼函數在單獨的'

0

聲明一個全局變量map的功能外,並從功能刪除var關鍵字。

var map; // global 
function InitializeGoogleMap() { 
try { 
      DirectionsDisplay = new google.maps.DirectionsRenderer(); 
      var LatitudeLongitude = new google.maps.LatLng(19.1969813, 72.9962491); 
      var GoogleMapOptions = 
      { 
       zoom: 10, 
       center: LatitudeLongitude, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map($("#me")[0], GoogleMapOptions); 

      DirectionsDisplay.setMap(map); 
      DirectionsDisplay.setPanel($("#DivDirectionRouteStatus")[0]); 
     } 
     catch (E) { 
      alert(E.message); 
     } 
    } 
function GetPlaces() { 
    try{ 
     var markers = 
      [<asp:Repeater ID="rptMarkers" runat="server"> 
         <ItemTemplate> 
            { 
             "title": '<%# Eval("Name") %>', 
             "lat": '<%# Eval("Latitude") %>', 
             "lng": '<%# Eval("Longatude") %>', 
             "description": '<%# Eval("Description") %>' 
            } 
         </ItemTemplate> 
         <SeparatorTemplate> 
          , 
         </SeparatorTemplate> 
         </asp:Repeater> 
     ]; 
     DirectionsDisplay = new google.maps.DirectionsRenderer(); 
     var mapOptions = { 
      center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
      zoom: 8, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     for (i = 0; i < markers.length; i++) { 
      var data = markers[i] 
      var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
       title: data.title 
      }); 
      (function (marker, data) { 
       google.maps.event.addListener(marker, "click", function (e) { 
        infoWindow.setContent(data.description); 
        infoWindow.open(map, marker); 
       }); 
      })(marker, data); 
     } 
    } 
    catch (E) 
    { 
     alert(E.message); 
    } 
} 
-2

我編輯了你的代碼。試試這個:

<script language="javascript" type="text/javascript"> 
var GoogleMapOptions = 
       { 
        zoom: 10, 
        center: LatitudeLongitude, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       }; 
var map = new google.maps.Map($("#me")[0], GoogleMapOptions); 
     function InitializeGoogleMap() { 

      try { 
       DirectionsDisplay = new google.maps.DirectionsRenderer(); 
       var LatitudeLongitude = new google.maps.LatLng(19.1969813, 72.9962491); 
       /*var GoogleMapOptions = 
       { 
        zoom: 10, 
        center: LatitudeLongitude, 
        mapTypeId: google.maps.MapTypeId.ROADMAP 
       };*/ 
       //commented below 
       //var map = new google.maps.Map($("#me")[0], GoogleMapOptions); 

       DirectionsDisplay.setMap(map); 
       DirectionsDisplay.setPanel($("#DivDirectionRouteStatus")[0]); 



      } 
      catch (E) { 
       alert(E.message); 
      } 



     } 




function GetPlaces() { 

     try{ 


      var markers = 
       [<asp:Repeater ID="rptMarkers" runat="server"> 
          <ItemTemplate> 
             { 
              "title": '<%# Eval("Name") %>', 
              "lat": '<%# Eval("Latitude") %>', 
              "lng": '<%# Eval("Longatude") %>', 
              "description": '<%# Eval("Description") %>' 
             } 
          </ItemTemplate> 
          <SeparatorTemplate> 
           , 
          </SeparatorTemplate> 
          </asp:Repeater> 
      ]; 

      DirectionsDisplay = new google.maps.DirectionsRenderer(); 


      var mapOptions = { 
       center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
       zoom: 8, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      //commented below line 
      // var map = new google.maps.Map($("#me")[0], mapOptions); 

      for (i = 0; i < markers.length; i++) { 
       var data = markers[i] 
       var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
       var marker = new google.maps.Marker({ 
        position: myLatlng, 
        map: map, 
        title: data.title 
       }); 
       (function (marker, data) { 
        google.maps.event.addListener(marker, "click", function (e) { 
         infoWindow.setContent(data.description); 
         infoWindow.open(map, marker); 
        }); 
       })(marker, data); 
      } 


     } 
     catch (E) 
     { 
      alert(E.message); 
     } 

     } 
    </script> 
+0

這不起作用。 'GoogleMapsOptions'在init函數中聲明。 –

+0

對不起。我編輯了我的代碼! –

+0

仍然無法正常工作。 –

相關問題