2017-02-16 39 views
0

我正在試圖爲我的Google地圖標記設置動畫。我設法對地圖進行樣式設置,設置自定義標記並獲得動畫效果,但有一些東西阻止它按我想要的方式工作。僅在視圖中放置Google地圖標記

第一件事是我使用pace.js添加一個加載欄到頁面。一旦加載頁面,CSS將基於CSS轉換的內容淡入視圖中。由於此轉換的持續時間爲0.5秒,因此動畫在頁面完全加載之前運行。所以基本上我需要一種向地圖標記動畫添加延遲的方法。

其次,我正在考慮將我的地圖放在網站的頁腳附近。這意味着沒有人會看到動畫。我認爲這將是很好,如果它只有在地圖可見(和頁面加載)動畫。

有沒有辦法實現這一目標?

這是我目前的代碼:

/* 
* When the window has finished loading create our google map below. 
*/ 

google.maps.event.addDomListener(window, 'load', init); 

/* 
* Basic options for a simple Google Map. For more options see: 
* https://developers.google.com/maps/documentation/javascript/reference#MapOptions 
* 
* 1. How zoomed in you want the map to start at (always required) 
* 2. The latitude and longitude to center the map (always required). 
* 3. How you would like to style the map. This is where you would paste any 
* style found on Snazzy Maps. 
* 4. Prevent map from being draggable. 
* 5. Hide map/satellite toggle. 
* 6. Hide "Street View" button. 
*/ 

function init() { 

    var mapOptions = { 
     zoom: 15, /* [1] */ 
     center: new google.maps.LatLng(LATVALUE, LONGVALUE), /* [2] */ 
     draggable: false, /* [4] */ 
     mapTypeControl: false, /* [5] */ 
     streetViewControl: false, /* [6] */ 
     styles: /* [3] */ 
      [{"stylers":[{"saturation":-100},{"gamma":1}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"off"}]},{"featureType":"all","elementType":"geometry.fill","stylers":[{"weight":"2.00"}]},{"featureType":"all","elementType":"geometry.stroke","stylers":[{"color":"#9c9c9c"}]},{"featureType":"all","elementType":"labels.text","stylers":[{"visibility":"on"}]},{"featureType":"landscape","elementType":"all","stylers":[{"color":"#f2f2f2"}]},{"featureType":"landscape","elementType":"geometry.fill","stylers":[{"color":"#ffffff"}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","elementType":"all","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"all","stylers":[{"saturation":-100},{"lightness":45}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"color":"#eeeeee"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#7b7b7b"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"road.highway","elementType":"all","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"transit","elementType":"fill","stylers":[{"weight":"1.00"}]},{"featureType":"transit","elementType":"labels.icon","stylers":[{"visibility":"on"},{"hue":"#a3cd39"},{"gamma":1},{"saturation":"50"}]},{"featureType":"water","elementType":"all","stylers":[{"color":"#46bcec"},{"visibility":"on"}]},{"featureType":"water","elementType":"geometry.fill","stylers":[{"color":"#c8d7d4"}]},{"featureType":"water","elementType":"labels.text.fill","stylers":[{"color":"#070707"}]},{"featureType":"water","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]}] 
    }; 

    /* 
    * Get the HTML DOM element that will contain your map. We are using a div with 
    * id="map" seen below in the <body> 
    */ 

    var mapElement = document.getElementById('map'); 

    /* 
    * Create the Google Map using our element and options defined above. 
    * 
    * 1. Map varible. 
    * 2. Marker variable so we can specify a retina image and resize. 
    */ 

    var map = new google.maps.Map(mapElement, mapOptions); /* [1] */ 
    var mapIcon = new google.maps.MarkerImage("img/interface/[email protected]", null, null, null, new google.maps.Size(100,78)); /* [2] */ 

    /* 
    * Let's also add a marker while we're at it. 
    */ 

    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(LATVALUE, LONGVALUE), 
     map: map, 
     flat: true, 
     title: 'COMPANY NAME', 
     icon: mapIcon, 
     animation: google.maps.Animation.DROP 
    }); 
} 

感謝您抽空閱讀,希望有人能幫助我的時候了! :)

回答

1

看看this answer。使用Waypoints可以在頁腳進入視圖時觸發一個功能。該函數一旦觸發將選擇標記並向其添加一個包含css轉換的類。

var waypoint = new Waypoint({ 
    element: $([selector for footer]), 
    handler: function() { 
    $([selector for marker]).addClass([class containing css transition]); 
    } 
}); 

元素告訴滾動過程中,觀察其航點DOM元素的位置,並處理是當該元素的排名靠前的視口頂部將觸發功能。

+0

我正在考慮使用航點,但我不確定你會如何去標記一個ID。您可以指定在JavaScript中使用的圖像,但看起來像Google地圖API會注入所有標記。 – user1406440

+0

在向Google地圖標記添加ID方面。看看這個[答](http://stackoverflow.com/a/3265918/7172409)。 –

相關問題