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
    St .Catharines Ontario
    Member #
    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...
    wp_register_script('googlemaps', '************', false, '3');
    The actual request/response
    $('.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();
    		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;
    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:
    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?


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 05:29 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: