Gmap3 - Forum

Gmap3 exchange platform

You are not logged in.

Ads
(server costs 27.50€ per month)


#1 2011-11-28 12:13:06

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Geting data from Direction Service

Hi can i obtain a lenght of route for each country?
that mean i go from Paris to Berlin and i receive

 200km in France , 850km in Germany 

that will be usefull because i will to add cost of route for trucks
bases on lenght but 1country = 1price
if not maybe you know the other options to calculate approximate cost of route

Offline

Ads
(server costs 27.50€ per month)


#2 2011-11-28 17:30:14

charlietfl
Moderator
Registered: 2011-11-24
Posts: 201

Re: Geting data from Direction Service

If you are looking for a simple few lines of code solution , I doubt there is one.

I believe you would have to reverse geocode all of the end point latLng of the route steps object just to get the country code for each. If you do this within Google API you'll likely run into query time limit issues so you would likely need an alternate geocoder source.

The route steps are fairly easy to loop over from the callback results using jQuery to get their coordinates, coordinating all the asynchronous reverse geocoding will be more involved

Offline

#3 2011-11-28 18:26:28

jbdemonte
Administrator
From: Pourrières, France
Registered: 2011-11-21
Posts: 580

Re: Geting data from Direction Service

Yep, i don't see how to get that without coding it from scratch,

like charlietfl said, you should look into the route, and sum the steps by country

Offline

#4 2011-11-28 21:13:04

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

So i have an idea
in direction panel we have the info "Entering: Germany"
if we can separate this point and set it as route end we will have the lenght in France (i.e)
next step set this point as start point and search for next "Entering:..." if not then lenght for Germany and end of loop
i must read about getting the info from panel but if you have any other idea (or any way to execute my way) i will be happy if you post here
Greetings From Poland

Offline

#5 2011-11-29 12:52:09

charlietfl
Moderator
Registered: 2011-11-24
Posts: 201

Re: Geting data from Direction Service

if you want to rely on the "Entering:" in the instructions text you wouldn't need to reverse geocode as I suggested

in the callback of 'getRoute' action the results object contains all the information you need. If you know how to use a browser console like Firebug , log the results object in the callback then look at routes/legs/steps and you will see the instructions for each step sent as an html string

Offline

#6 2011-11-29 18:40:02

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

alert(result.routes[0].legs[0].steps[0].instructions);
This works for my test map (clean js with form to set direction)
i need to do that (for begining) to my map with gmap3 script

{ action:'addDirectionsRenderer',
		        panelId: 'wskazowki',
		        options:{      
		          preserveViewport: true,
		          draggable: true,
		          directions:results
		      }
		      }

i wish that alert will show every time i change my direction panel (green marker grab)

Offline

#7 2011-11-29 19:51:38

charlietfl
Moderator
Registered: 2011-11-24
Posts: 201

Re: Geting data from Direction Service

I have an app where I get directions every time a marker is dragged, but I don't display them on map. I need to verify that directions can be created. In some remote areas they are not. I also want to know the distance from marker to beginning of directions. If marker is in a forest or in mountains and not on a main road this can be as much as 3 miles( 5km).
I bind this to 'addMarker' action  , and use dragend event. I do it similar to this (modified it a bit to find step for "Entering Germany"):

$(document).ready(function() {

    var berlin = [52.4982586, 13.3484535];
    var paris = [48.8620722, 2.352047];

    $('#test2').gmap3({
        action: 'init',
        zoom: 7,
        center: paris
    }, {
        action: 'addMarker',
        options: {
            draggable: true
        },
        latLng: paris,
        events: {
            dragend: function(marker) {
                $(this).gmap3({
                    action: 'getRoute',
                    options: {
                        origin: marker.getPosition(),
                        destination: berlin,
                        travelMode: 'DRIVING'
                    },
                    callback: function(results) {
                        if (results) {
                            var steps = results.routes[0].legs[0].steps
                            $.each(steps, function(i, step) {
                                if (/Entering Germany/.test(step.instructions)) {
                                    alert('Enter Germany at step index #' + i)
                                }
                            })

                        }
                    }

                })
            }
        }
    })

});

Offline

#8 2011-11-29 21:25:26

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

that should be it but i have no idea how to modify my function
to do this callback and you code don`t working for me (no alerts no directions)
but that is so close
Code after trying modification

$(function(){
	$('#mapka').gmap3({ 
		  action:'getRoute',
		  options:{
		origin:origin,
	    destination:destination,
		    travelMode: google.maps.DirectionsTravelMode.DRIVING
		  },
		  
		  
			  callback: function(results) {
			  alert("Callback init");
				  if (results) {
				  var steps = results.routes[0].legs[0].steps
				  $.each(steps, function(i, step) {
				  if (/Entering Germany/.test(step.instructions)) {
				  alert('Enter Germany at step index #' + i)
				  }
				  })
				   
				  }
				  
    
		    $(this).gmap3(
		      { action:'init',  ..... 

EDIT : That works for me Thank you
Now i must insert this to marker drag funtion (but that green marker of directions)
and change that code to receive usefull information and i am sure that will be more questions
THX again

Last edited by butcherfl (2011-11-29 21:34:34)

Offline

#9 2011-11-29 22:36:04

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

1 add question for today
How to put geocoding to display the result in document (document.write(result))??
Good Night

Last edited by butcherfl (2011-11-29 22:36:16)

Offline

#10 2011-11-29 23:36:28

charlietfl
Moderator
Registered: 2011-11-24
Posts: 201

Re: Geting data from Direction Service

you can chain actions inside gmap3... which is an awesome feature of the plugin

callback :function(results){

          // parse the results object to get steps information- "Entering..."

          //  add directions to map and display text of steps
          $('#directions').empty() //  empty previous directions from id=directions
           $(this).gmap3({
                action: 'setDirections',// add directions to map
                directions: results
            },{
                action: 'setDirectionsPanel',  // uses google API to write the directions
                id: 'directions' // id of div where you want directions displayed
            });

}

You could also parse the steps.instructions yourself if you needed to and use jQuery methods to insert the text/html instead of using google setDirectionsPanel()

Offline

#11 2011-11-30 11:51:53

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

Hmm that is your function in my code

callback :function(results){
				     alert("!!");
					// parse the results object to get steps information- "Entering..."
					 
					//  add directions to map and display text of steps
					$('#wskazowki').empty() //  empty previous directions from id=directions
					$(this).gmap3({
					action: 'setDirections',// add directions to map
					directions: results
					},{
					action: 'setDirectionsPanel',  // uses google API to write the directions
					id: 'wskazowki' 
				      // id of div where you want directions displayed
					});
					 
					}

i need to call function every time i drag the green marker(for start simply alert) i think this can be before action:'setDirectionsPanel'
but i have no idea how to paste that function here

Offline

#12 2011-11-30 12:53:16

charlietfl
Moderator
Registered: 2011-11-24
Posts: 201

Re: Geting data from Direction Service

the callback is the same callback from 'getRoute' action...  I even commented where to run your steps parsing code

Offline

#13 2011-11-30 19:11:32

jbdemonte
Administrator
From: Pourrières, France
Registered: 2011-11-21
Posts: 580

Re: Geting data from Direction Service

butcherfl wrote:

i need to call function every time i drag the green marker(for start simply alert) i think this can be before action:'setDirectionsPanel'
but i have no idea how to paste that function here

Use the marker event like in the demo zip :

$('#test8').gmap3({
    action: 'addMarker',
    latLng: position,
    map:{
      center: position,
      zoom: 8
    },
    marker:{
      options:{
        draggable: true,
        icon: 'http://code.google.com/intl/fr/apis/maps/documentation/javascript/examples/images/beachflag.png'
      },
      events:{
        dragend: function(marker, event){
          $('#test8-result .result-1').html( marker.position.lat() );
          $('#test8-result .result-2').html( marker.position.lng() );
        }
      }
    }
  },
...

Offline

#14 2011-11-30 20:35:34

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

ok i can do this but is possible to create dragend event
to markers from routeDirections (that green "A" and "B")?

Something like this but working smile

 { action:'addDirectionsRenderer',
		    	   panelId: 'wskazowki',
			        options:{      
			          preserveViewport: true,
			          draggable: true,
			          directions:results
			      },
			      events:{
			    	  dragend: function(marker, event){
			    	  $('#test').html( marker.position.lat() );
			    	  $('#test').html( marker.position.lng() );
			    	  }
			}

I ask different : How to call any function (i.e alert) every time i change my directions in different way (marker drag route drag etc.)?

Last edited by butcherfl (2011-12-02 00:20:41)

Offline

#15 2011-12-06 11:32:07

butcherfl
Member
Registered: 2011-11-22
Posts: 21

Re: Geting data from Direction Service

i have a little problem with compare Strings from steps.instruction

if (/<b>A11<\/b>/.test(step.instructions)) {

i want to change the A11 to A[0-99]
and my ask continous
How to call action everytime i change my directions (something like onchange())

Offline

#16 2011-12-12 16:25:09

Tadek888
Member
Registered: 2011-12-12
Posts: 10

Re: Geting data from Direction Service

Hello smile
This part works perfect. But if i want to put coordinates ( latitude and langitude ) into input value what should i change ? smile

{ action:'addDirectionsRenderer',
panelId: 'wskazowki',
options:{     
preserveViewport: true,
draggable: true,
directions:results
},
events:{
dragend: function(marker, event){
$('#test').html( marker.position.lat() );
$('#test').html( marker.position.lng() );
}
}

Offline

#17 2011-12-12 17:10:43

Tadek888
Member
Registered: 2011-12-12
Posts: 10

Re: Geting data from Direction Service

$('#test').val( marker.position.lat() );

smile

Offline

#18 2016-02-26 14:19:35

game-overe@hotmail.com
Member
Registered: 2016-02-26
Posts: 1

Re: Geting data from Direction Service

Hello,
I want to do the same thing as butcherfl but this solution is not valide for all the countries because when i choose Morocco as start point to an other country the instructions doesn't contains the information "Entering: xxxx" pleaze i need your help.
thank you.

Offline

#19 2016-12-06 22:44:24

Andrewsep
Member
From: Malawi
Registered: 2016-11-19
Posts: 11
Website

Re: Geting data from Direction Service

I am also having the unable to read trial data issue. I have also tried uninstalling and reinstalling. I however have no idea where to go from here.

Offline

#20 2016-12-07 07:58:21

Andrewsep
Member
From: Malawi
Registered: 2016-11-19
Posts: 11
Website

Re: Geting data from Direction Service

Just had a computer lockup here and blue screen I don't think it was PGO related, but I'm having the same "unable to read trial data" error message.

I've tried 'repair' and reinstall, but no joy.

Anyone else figure this out?  I'm running the latest version from the site 4-8-821

Thanks

Offline

Board footer

Powered by FluxBB