0

I dug up this code from the ACF examples for Google maps markers:

.acf-map {
  width: 100%;
  height: 400px;
  border: #ccc solid 1px;
  margin: 20px 0;
}

</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
(function($) {

/*
*  render_map
*
*  This function will render a Google Map onto the selected jQuery element
*
*  @type  function
*  @date  8/11/2013
*  @since 4.3.0
*
*  @param $el (jQuery element)
*  @return  n/a
*/

function render_map( $el ) {

  // var
  var $markers = $el.find('.marker');

  // vars
  var args = {
    zoom    : 16,
    center    : new google.maps.LatLng(0, 0),
    mapTypeId : google.maps.MapTypeId.ROADMAP
  };

  // create map           
  var map = new google.maps.Map( $el[0], args);

  // add a markers reference
  map.markers = [];

  // add markers
  $markers.each(function(){

      add_marker( $(this), map );

  });

  // center map
  center_map( map );

}

/*
*  add_marker
*
*  This function will add a marker to the selected Google Map
*
*  @type  function
*  @date  8/11/2013
*  @since 4.3.0
*
*  @param $marker (jQuery element)
*  @param map (Google Map object)
*  @return  n/a
*/

function add_marker( $marker, map ) {

  // var
  var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

  // create marker
  var marker = new google.maps.Marker({
    position  : latlng,
    map     : map
  });

  // add to array
  map.markers.push( marker );

  // if marker contains HTML, add it to an infoWindow
  if( $marker.html() )
  {
    // create info window
    var infowindow = new google.maps.InfoWindow({
      content   : $marker.html()
    });

    // show info window when marker is clicked
    google.maps.event.addListener(marker, 'click', function() {

      infowindow.open( map, marker );

    });
  }

}

/*
*  center_map
*
*  This function will center the map, showing all markers attached to this map
*
*  @type  function
*  @date  8/11/2013
*  @since 4.3.0
*
*  @param map (Google Map object)
*  @return  n/a
*/

function center_map( map ) {

  // vars
  var bounds = new google.maps.LatLngBounds();

  // loop through all markers and create bounds
  $.each( map.markers, function( i, marker ){

    var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );

    bounds.extend( latlng );

  });

  // only 1 marker?
  if( map.markers.length == 1 )
  {
    // set center of map
      map.setCenter( bounds.getCenter() );
      map.setZoom( 16 );
  }
  else
  {
    // fit to bounds
    map.fitBounds( bounds );
  }

}

/*
*  document ready
*
*  This function will render each map when the document is ready (page has loaded)
*
*  @type  function
*  @date  8/11/2013
*  @since 5.0.0
*
*  @param n/a
*  @return  n/a
*/

$(document).ready(function(){

  $('.acf-map').each(function(){

    render_map( $(this) );

  });

});

})(jQuery);
</script>

What I'd like to do now is draw routes between each marker (routes from marker 1 to marker 2, from marker 2 to marker 3, from marker 3 to marker 4 and so on). Every step except the last one - I don't want to connect marker 5 with marker 1 for example if marker 5 is the last available marker.

closed as off-topic by Pieter Goosen, Milo, Mayeenul Islam, Wyck, Mark Kaplun Jan 28 '15 at 4:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Correct me if I'm wrong but your request seems not very WordPress related to me, even if you use the WordPress plugin ACF and the Google Map add-on (you'll need also the premium add-on Repeater Field). These plugins/add-ons make it easier for you to insert the geocodes in the back-end. On the front-end side it is just a list of points and to connect these points you should read the documentation of the Google Maps API in my opinion. – realloc Jan 26 '15 at 14:21
  • It's more Wordpress related than anything else I think because of the plugin/example above. I mean most of it is just JS and PHP, but I thought someone else might have a complete example that's already working. I used the maps API to no avail. By the way, I do have ACF pro and the points are currently showing, but I just need to connect the routes. – twenty Jan 26 '15 at 15:03
0

I've managed to work around this by using Gmaps.js.

function initMap($el) {
  var bounds = [];
  var waypts = [];
  var $markers = $($el).find('.marker');

  var map = new GMaps({
    div: $el,
    scrollwheel: false,
    zoomControl : true,
    zoomControlOpt: {
      style: 'LARGE',
      position: 'TOP_RIGHT'
    },
    panControlOpt: {
      position: 'TOP_RIGHT'
    }
  });

  $markers.each(function() {
    // DEFINE CURRENT/NEXT MARKER
    $this = $(this);
    $next = $(this).next();

    // DEFINE BOUNDS
    var latlng = new google.maps.LatLng($(this).data('lat'), $(this).data('lng'));
    bounds.push(latlng);

    if( $next.length != 0 ) {
      waypts.push({
        location: $next .find('.address').html(),
        stopover: true
      });
    }

    // MARKERS
    map.addMarker({
      lat: $(this).data('lat'),
      lng: $(this).data('lng'),
      icon: $(this).data('icon'),
      infoWindow: {
        content: $(this).html()
      }
    });
  });

  map.drawRoute({
    origin: [$markers.first().data('lat'), $markers.first().data('lng')],
    destination: [$markers.last().data('lat'), $markers.last().data('lng')],
    waypoints: waypts,
    travelMode: 'driving',
    strokeColor: '#F1594D',
    strokeOpacity: 1,
    strokeWeight: 4
  });

  // STYLE
  var styles = [{featureType:'water',stylers:[{color:'#70B6E9'},{visibility:'on'}]},{featureType:'landscape',stylers:[{color:'#E8E1D9'}] },{featureType:'road',stylers:[{saturation:-100},{lightness:45}]},{featureType:'road.highway',stylers:[{visibility:'simplified'}] },{featureType:'road.arterial',elementType:'labels.icon', stylers:[{visibility:'off'}] },{featureType:'administrative',elementType:'labels.text.fill',stylers:[{color:'#444444'}] },{featureType:'transit', stylers:[{visibility:'off'}] },{featureType:'poi', stylers:[{visibility:'off'}] }];

  map.addStyle({
    styledMapName:"Styled Map",
    styles: styles,
    mapTypeId: "map_style"  
  });

  map.setStyle("map_style");
  map.fitLatLngBounds(bounds);
} 

$('.js-TourMap').each(function() {
  initMap('#' + $(this).attr('id'));
})
  • Care to share where you added that code, @twenty? Does this replace the JS ACF provides in their documentation, or supplement it? – Nathan Sep 2 '16 at 19:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.