Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Senior Member brandMatt's Avatar
    Join Date
    Oct 2015
    Location
    St .Catharines Ontario
    Posts
    240
    Member #
    52164
    Liked
    51 times

    add context (this) to google maps request

    I cannot seem to use context inside a distance matrix call. My code is very close to the sample that google gives for their map api. I am running this code inside of Wordpress, so I have enqued/registered my API as a script.

    I don't think the issue is in my first block of code, but I am open to any and all advice. Please help guys.

    Inside my functions...
    Code:
    wp_register_script('googlemaps', 'http://maps.googleapis.com/maps/api/js?key=************', false, '3');
    wp_enqueue_script('googlemaps');
    The actual request/response
    Code:
    $('.houseWrapper').each(function() {
    	var origin = $(this).children('.houseMeta').attr('data-origin');
    	var destination = $(this).children('.houseMeta').attr('data-destination');
    	
    	var service = new google.maps.DistanceMatrixService();
    	service.getDistanceMatrix({
    		origins: [origin],
    		destinations: [destination],
    		travelMode: google.maps.TravelMode.DRIVING,
    		unitSystem: google.maps.UnitSystem.METRIC,
    		avoidHighways: false,
    		avoidTolls: false,
    	}, callback);
    
    	function callback(response, status) {
    		if (status == google.maps.DistanceMatrixStatus.OK) {
    			var origins = response.originAddresses;
    			var destinations = response.destinationAddresses;
    
    			for (var i = 0; i < origins.length; i++) {
    				var results = response.rows[i].elements;
    				for (var j = 0; j < results.length; j++) {
    					var element = results[j];
    					var distance = element.distance.text;
    					var duration = element.duration.text;
    					
    					console.log($(this).html());
    				}
    			}
    		}
    	}
    });
    Update:
    One possible solution is to save this as a variable before the request to access it later.
    Last edited by brandMatt; Nov 29th, 2015 at 02:50 PM.

    My Web Site:
    brandwebdesign.ca
    If someone helped you out, be sure to "Like" their post and/or help them in kind. The "Like" link is on the bottom right of each post, beside the "Share" link. Some of us are really insecure and need those likes so that we feel important and smart, so come on, help us out, huh?

  2.  


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:44 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com