-
3 Jul 2012 11:13 PM #1
Unanswered: Set distance from a place in a NestedList by current position
Unanswered: Set distance from a place in a NestedList by current position
Hi, I’ve a Nested List retrieved from a json file. I use getItemTextTpl to show specific field of the elements.
Each leaf element has longitude and latitude fields: i want to display the distance by the places from the current position.
I’ve tried to retrieve current position inside getItemTextTpl method by creating a Geolocation object, but it doesn’t work. I couldn’t obtain longitude and latitude from that object outside locationupdate method.
If i try to declare long and lat vars before creating Geolocation object and try to set them inside locationupdate method of Geolocation object, them aren’t set to the values that i retrieve from geo.getLatitude() and geo.getLongitude().
What i want to do is to display in the last level (the level before detailCard) the distance by a place from current position retrieved by an Geolocation object.
How could I do it?
-
5 Jul 2012 12:15 PM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,623
- Vote Rating
- 434
- Answers
- 3103
So you need to lat/long coords from within the locationupdate event but you can't? The reason is because the locationupdate event probably is firing before your nestedlist is created maybe?
Mitchell Simoens @SenchaMitch
Sencha Inc, Senior Forum Manager
________________
http://www.JSONPLint.com - Source to lint your JSONP!
Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
https://github.com/mitchellsimoens
Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/
Need more help with your app? Hire Sencha Services services@sencha.com
Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!
When posting code, please use BBCode's CODE tags.
-
6 Jul 2012 12:14 AM #3
My code is:
My problem was to register Lat and Lon variables outside of the locationupdate method.Code:var geo = Ext.create('Ext.util.Geolocation', { autoUpdate: false, listeners: { locationupdate: function(geo) { calculateDistances(geo.getLatitude(), geo.getLongitude(), lat, lon, record); }, locationerror: function(geo, bTimeout, bPermissionDenied, bLocationUnavailable, message) { if(bTimeout){ alert('Timeout occurred.'); } else { alert('Error occurred.'); } } } }); geo.updateLocation();
Now I've changed something: I use Google "Distance Matrix Service" to calculate distances.
A portion of the JS code is as follows:
Code:function calculateDistances(lat1, lon1, lat2, lon2, record) { var service = new google.maps.DistanceMatrixService(); var origin1 = new google.maps.LatLng(lat1, lon1); var destinationB = new google.maps.LatLng(lat2, lon2); service.getDistanceMatrix( { origins: [origin1], destinations: [destinationB], travelMode: google.maps.TravelMode.WALKING, unitSystem: google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false }, callback); } function callback(response, status) { if (status != google.maps.DistanceMatrixStatus.OK) { alert('Error was: ' + status); } else { var origins = response.originAddresses; var destinations = response.destinationAddresses; deleteOverlays(); for (var i = 0; i < origins.length; i++) { var results = response.rows[0].elements; for (var j = 0; j < results.length; j++) { var tempDist = results[0].distance.text; console.log(tempDist); } } } }
In callback function with console.log(tempDist), I can view in console the calculated distance.
My problem now is that I can't return this value to calculateDistances method where I can access to a record and modify it with the calculated value
-
19 Feb 2013 4:27 AM #4


Reply With Quote