-2

對於我而言,我無法完成這項工作。我正在嘗試將標記語句集合傳遞給我的Google地圖 - 但它不起作用。我已經通過Google Maps API設置了此地圖,並且它是針對Wordpress網站的。用戶可以使用「自定義帖子」類型添加位置。在很近的地方有很多地方,所以我試圖用MarkerCluster來減輕一點。我以前從來沒有使用過MarkerCluster,所以它也可能是一件小事......不確定。 'var mc = new MarkerClusterer(map);' - 這個錯誤提示:「Uncaught ReferenceError:MarkerClusterer is not defined」Google地圖標記羣集不起作用

有沒有什麼見解?任何幫助是極大的讚賞!!

<?php // Index template 
get_header(); ?> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 




<div class="twelve column"> 
     <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

      <div class="intro"> 
       <?php the_excerpt(); ?> 
       <hr> 
      </div> 

       <?php the_content(); ?> 

       <header class="clearfix"></header> 
         <div id="mapcanvas"></div> 
           <?php 
           // For creating multiple, customized loops. 
           // http://codex.wordpress.org/Class_Reference/WP_Query 
           $custom_query = new WP_Query('post_type=locations'); // exclude category 9 
           while($custom_query->have_posts()) : $custom_query->the_post(); ?> 
            <?php if(get_field('link')): ?> 
             <div> 
             <?php while(has_sub_field('link')): ?> 
              <div> 
               <p><?php the_sub_field('url'); ?></p> 
              </div> 
             <?php endwhile; ?> 
             </div> 
            <?php endif; ?> 
           <?php endwhile; ?> 
           <?php wp_reset_postdata(); // reset the query ?> 

           <?php 
           function get_single_term($post_id, $taxonomy) 
           { 
            $terms = wp_get_object_terms($post_id, $taxonomy); 
            if(!is_wp_error($terms)) { 
            return '<a href="'.get_term_link($terms[0]->slug, $taxonomy).'">'.$terms[0]->name.'</a>'; 
            } 
           } 

           $i = 0; 

           // For creating multiple, customized loops. 
           // http://codex.wordpress.org/Class_Reference/WP_Query 
            $custom_query = new WP_Query('post_type=location&posts_per_page=-1'); 
            while($custom_query->have_posts()) : $custom_query->the_post(); 

             $title = get_the_title(); // Location title 
             $map = get_field('location'); // ACF location contains address and coordinates 
             $terms = strip_tags(get_the_term_list($post->ID, 'distributors', '', ' & ')); // Get distributor terms and rm links 
             $info = '<strong>' . $title . '</strong><br>' . $map['address']; // Info window content 
             $link = get_field('link'); 
             if($link){ 
             $info .= '<br><a href="http://'. $link .'">'. $link .'</a>'; 
             } 
             $location[$i][0] = $title; // Store the post title 
             $location[$i][1] = $map['coordinates']; // Store the ACF coordinates 
             $location[$i][2] = json_encode($info); // Store info window content 
             $location[$i][3] = strip_tags(get_single_term($post->ID, 'distributors')); // Get first term for marker icon 

             $i ++; 
            endwhile; ?> 
            <?php wp_reset_postdata(); // reset the query ?> 

           <script> 
           $(function initialize() { 
           geocoder = new google.maps.Geocoder(); 
            // Center map on our main location 
            var myLatLng = new google.maps.LatLng(41.583013,-93.63701500000002); 
            var bounds = new google.maps.LatLngBounds(); 

            // https://developers.google.com/maps/documentation/javascript/styling 
            // Create an array of styles. 
            var styles = [ 
            { 
             stylers: [ 
             { saturation: -99.9 } 
             ] 
            } 
            ]; 

            // Create a new StyledMapType object, passing it the array of styles, 
            // as well as the name to be displayed on the map type control. 
            var styledMap = new google.maps.StyledMapType(styles, {name: 'exile'}); 

            // Create a map object, and include the MapTypeId to add 
            // to the map type control. 
            var mapOptions = { 
            mapTypeId: 'roadmap', 
            center: myLatLng, 
            zoom: 14, 
            disableDefaultUI: false, 
            scrollwheel: true, 
            draggable: true 
            }; 

            // Display a map on the page 
            var map = new google.maps.Map(document.getElementById("mapcanvas"), mapOptions); 
            var mc = new MarkerClusterer(map); 
            map.setTilt(45); 

            //Associate the styled map with the MapTypeId and set it to display. 
            map.mapTypes.set('exile', styledMap); 
            map.setMapTypeId('exile'); 

           // Marker icons 
            typeObject = { 
            "Others" : { 
             "icon" : new google.maps.MarkerImage('http://exilebrewing.com/site/img/beer-mug.png', new google.maps.Size(18,26), new google.maps.Point(0,0), new google.maps.Point(9,26)), 
             "shadow" : new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png', new google.maps.Size(40,34)) 
            } 
            } 

            // http://wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/ 
            // Multiple Markers 
            var markers = [ 
            ["Exile Brewing Co", 41.583013,-93.63701500000002,"Others"], 
            <?php 
            if (count($location)>0) { 
             foreach ($location as $key => $value){ 
             if ($key < (count($location)-1)){ 
              echo '["' . $location[$key][0] . '",' . $location[$key][1] . ',"' . $location[$key][3] . '"], ' . "\n"; 
             } else { 
              echo '["' . $location[$key][0] . '",' . $location[$key][1] . ',"' . $location[$key][3] . '"]'; 
             } 
             } 
            } 
            ?> 
            ]; 
            var markers = []; 
            for (var i = 0; i < 100; i++) { 
            var latLng = new google.maps.LatLng(data.photos[i].latitude, 
             data.photos[i].longitude); 
            var marker = new google.maps.Marker({'position': latLng}); 
            markers.push(marker); 
            } 
            var markerCluster = new MarkerClusterer(map, markers); 

            // Info Window Content 
            var infoWindowContent = [ 
            ["<strong>Exile Brewing Co.</strong><br>1514 Walnut Street, Des Moines"], 
            <?php 
            if (count($location)>0) { 
             foreach ($location as $key => $value){ 
             if ($key < (count($location)-1)) { 
              echo '[' . $location[$key][2] . '], ' . "\n"; 
             } else { 
              echo '[' . $location[$key][2] . ']'; 
             } 
             } 
            } 
            ?> 
            ]; 

            // Display multiple markers on a map 
            var infoWindow = new google.maps.InfoWindow(), marker, i; 

            // Loop through our array of markers & place each one on the map 
            for(i = 0; i < markers.length; i++) { 
            var position = new google.maps.LatLng(markers[i][1], markers[i][2]); // ACF coordinates 
            var icon = typeObject[markers[i][3]]['icon']; 
            var shadow = typeObject[markers[i][3]]['shadow']; 
            bounds.extend(position); 
            marker = new google.maps.Marker({ 
             position: position, 
             map: map, 
             title: markers[i][0], 
             icon: icon, 
             shadow: shadow 
            }); 

            // Allow each marker to have an info window 
            google.maps.event.addListener(marker, 'click', (function(marker, i) { 
             return function() { 
             infoWindow.setContent(infoWindowContent[i][0]); 
             infoWindow.open(map, marker); 
             } 
            })(marker, i)); 

            // Automatically center the map fitting all markers on the screen 
            //map.fitBounds(bounds); 
            } 

            // Override our map zoom level once our fitBounds function runs (Make sure it only runs once) 
            var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) { 
            this.setZoom(15); 
            google.maps.event.removeListener(boundsListener); 
            }); 

           }); 
           </script> 

           <div id="map-canvas"></div> 


       <footer class="clearfix"></footer> 
    <?php endwhile;?> 
    <?php endif; ?> 
    </div> 
</div> 

<?php get_footer(); ?> 
+0

此外,這裏是任何其他參考網站的網址:http://exilebrewing.com/locations/ – alliefayden

+0

老兄,我很抱歉,它可能只是我,這可能是因爲我累了或出於重點 - 。但是我看不到你的頁面上有'markerclusterer'腳本。你沒有包括它 –

+0

@ObmerkKronen我肯定會錯過一些東西......我以前沒有用過這個!是否有特定的腳本?我在評論後說://在'var mc = new MarkerClusterer(map);'在頁面上顯示地圖' – alliefayden

回答

0

如果您想使用它,您需要包含第三方腳本marker clusterer

正如文件中規定:

Note: Be sure to include markerclusterer.js or markerclusterer_packed.js in your HTML document.

<script src="/path/to/markerclusterer.js" type="text/javascript"></script> 
1

按我的意見:

您需要下載腳本(here),然後在你的頁面包含它。

標記聚類器是不是谷歌地圖的built-in功能,但一個addon腳本..

<script src="markerclusterer.js" type="text/javascript"></script> 

或 「正確」 的WordPress的方式:

wp_register_script('js_marker_clusterer', plugins_url('/scripts/MarkerClusterer.js', __FILE__),'jquery'); // or other dependencies descendents - preferably map JS or your custom. 
wp_enqueue_script('js_marker_clusterer'); 

(改變你的路)

另外,看看例子(HERE)並檢查他們的代碼(包括)

+0

非常感謝,我已經把它連接起來了。還有一個錯誤,儘管找不到某些東西......我從Cluster文檔的這段代碼中得到了一段代碼的其餘部分的錯誤:「Uncaught ReferenceError:data is not defined」: – alliefayden

+0

'var latLng = new google.maps.LatLng(data.photos [i] .latitude, data.photos [i] .longitude); var marker = new google.maps.Marker({'position':latLng}); markers.push(marker); } var markerCluster = new MarkerClusterer(map,markers); ' – alliefayden

+0

我需要加載羣集圖標圖像嗎? – alliefayden