Tommy B
Posts: 0
Joined: Sat Sep 15, 2012 3:39 am

Geocoding app question

I am creating a proof of concept using your "Building a geocoding app with Appery.io database and server code" example. My trouble is that I have the lat/long stored in the database and not an address because I want to reference building locations within a large complex and not physical address's.

Getting to my question: can you assist me with the js code to allow me to reference lat/long parameters and not a street address or city?

code
// Database Id
var dbId = "your database id&quot //Change this value to your database id

var responseBody = {};

// Get request parameters
var latitude = request.object().latitude;
var longitude = request.object().longitude;
var radius = request.object().radius;

// The function that calculates the distance between two points

function getDistance(lat1, lat2, lon1, lon2) {

var R = 3959; // Earth radius in miles

var dLat = (lat2 - lat1)*Math.PI/180;

var dLon = (lon2 - lon1)*Math.PI/180;

var lat1 = lat1*Math.PI/180;

var lat2 = lat2*Math.PI/180;

var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

var d = R * c;

return d;

}

try {

// Check if all the necessary parameters are present and valid

if (latitude && longitude && radius && !isNaN(latitude) && !isNaN(longitude) && !isNaN(radius)) {

var results = [];

var params = {};

// Sort the results by contact name

params.sort = "contactName&quot

// Get all customers from the database

var customers = Collection.query(dbId, "customers", params);

for (var i = 0; i < customers&#46;length; i++) {

&#47;&#47; Geocode customer's address with Google API

var XHRResponse = XHR&#46;send("GET", "https:&#47;&#47;maps&#46;googleapis&#46;com/maps/api/geocode/json", {

"parameters": {

"address": customers&#46;address,

"sensor": "false"

}

});

console&#46;log(XHRResponse);

if (XHRResponse&#46;status == 200) {

&#47;&#47; Get coordinates from the response

var lat = JSON&#46;parse(XHRResponse&#46;body)&#46;results[0]&#46;geometry&#46;location&#46;lat;

var lng = JSON&#46;parse(XHRResponse&#46;body)&#46;results[0]&#46;geometry&#46;location&#46;lng;

&#47;&#47; If the customer's coordinates are within the radius,

&#47;&#47; add the customer record to the results array

if (getDistance(lat, latitude, lng, longitude) <= radius) {

results&#46;push(customers);

}

}

}

responseBody["results"] = results;

response&#46;success(responseBody, "application/json");

} else {

response&#46;error("Wrong parameters", 400);

}

} catch (e) {

console&#46;log(e);

response&#46;error("An error occured", 400);

}

/code

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Geocoding app question

Tommy, not going to weigh in too heavily here ( save that for the real experts) but I'm building a golf geo app ... And if suggest you store the geo points in the database as type GEO ... It's easier to store them, and you can query the db based on a radius from a geo point quickly and easily. The canned tutorial doesn't do that ... But if you need code snippets and sample query where clauses against a db with geo points in it ... I can share that with you... Suggested reading ... Mongodb reference material on searching based on geo point... I can share that link if you like as well.

Good luck !!!

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Geocoding app question

Hello!

@Bruce - thanks for comment :)

@Tommy - please let us know if you need support team help.

Tommy B
Posts: 0
Joined: Sat Sep 15, 2012 3:39 am

Geocoding app question

Bruce, thanks for the comments. I will do some studying on the mongodb this weekend.
Maryna,
The app that you have at the following link is what I need.

http://devcenter.appery.io/tutorials/...

But I want to do is check against latitude and longitude that is stored in the db and not an address or city. So I won't need to convert the address to lat/ long as shown in the tutorial but I'm needing assistance with changing the js code that is referenced in tutorial.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Geocoding app question

Hi Tommy B,

You just need to get geopoints from DB. Then iterate them and create marker for each geopoint from list result.

Here is an example of "CreateMarker" function and using of it:

pre

Code: Select all

 &#47;&#47;Were "multiGoogleMap" is map component name&#46; 
 var map = Appery("multiGoogleMap")&#46;options&#46;mapElement&#46;gmap('get', 'map'); 

&#47;&#47;Create infowindow&#46;
var infowindow = new google&#46;maps&#46;InfoWindow({
content: 'test content',
maxWidth: 320
});

Code: Select all

 var CreateMarker = function(data){ 
     var marker = new google&#46;maps&#46;Marker({ 
         position: new google&#46;maps&#46;LatLng(data&#46;lat, data&#46;long), 
         map: map, 
         draggable: true, 

         title: data&#46;location 
     }); 

     &#47;&#47;Add event handler and open infoWindow 
     google&#46;maps&#46;event&#46;addListener(marker, 'click', function() { 
         &#47;&#47;Set content for infowindow&#46; 
         infowindow&#46;setContent('Content of + ' + data&#46;location + ''); 

         &#47;&#47;Open infowindow&#46; 
         infowindow&#46;open(map, marker); 
     }); 
 }; 

 &#47;&#47;Here you should iterate your response geopoints and create marker for each of them&#46; 
 for (var i = 0; i < locationHelper&#46;aLocations&#46;length; i++) 
     &#47;&#47;In this function you should to pass latitude, longitude and text to display in infowindow&#46; 
     CreateMarker({lat: locationHelper&#46;aLocations[i][0], long: locationHelper&#46;aLocations[i][1], location:  locationHelper&#46;aLocations[i][2]}); 

/pre

Bruce, good suggestion :)

Regards.

Eri
Posts: 0
Joined: Wed Jun 17, 2015 8:51 pm

Geocoding app question

PLEASE I NEED TO GET THE DISTANCE TO SHOW IN MY APP. HOW CAN i SHOW IT ?

if (XHRResponse.status == 200) {

// Get coordinates from the response

var lat = JSON.parse(XHRResponse.body).results[0].geometry.location.lat;

var lng = JSON.parse(XHRResponse.body).results[0].geometry.location.lng;

// If the customer's coordinates are within the radius,

// add the customer record to the results array

if (getDistance(lat, latitude, lng, longitude) <= radius) {

results.push(customers);

}

}

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Geocoding app question

Hi Eri,

Unfortunatly it's not clear about your problem.

You have result but don't know how to show it to user?

If so you need:

1 Put label component on the page.

2 Use following JS code where you need to populate label component with your dynamic data:

pre

var dynamicValueToShow = "here you need to set your own value for example distance";

Apperyio("labelName")&#46;text(dynamicValueToShow);

/pre

Regards.

mami
Posts: 0
Joined: Thu Nov 05, 2015 12:52 am

Geocoding app question

As he said Bruce , I want use Geo data and use $near but I don't know where to put the request because the current geolocation is a variable .
thank you
Mami

Howard Chang
Posts: 0
Joined: Mon Nov 16, 2015 6:05 am

Geocoding app question

Hi Bruce, I am build my app and need exactly what you mentioned above. I would like to read the geopoint from database instead of doing distance calculation every time. Can you share the code snippets? Really appreciate it.

Return to “Issues”