2

I am working with Goole map in a VF page.Consider that Account object has Geolocation field. Based on the user location I have mapped the center point in the map and got the bounds of the map using getBounds() function in javascript.

GetBounds returns two Geolocations SouthWest and NorthEast.

How I can query all the accounts within those two geolocations?

2

If you can get a latitude/longitude pair from those Geolocations on the page, you should be able to find records within a radius of those points via the DISTANCE and GEOLOCATION functions used in a WHERE clause. The specifics of these functions are discussed in the Geolocation based SOQL Queries documentation.

If the release notes are correct, you can preform queries such as the ones below:

Double myLatitude = 10; 
Double myLongitude = 10;
List accountList = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(:myLatitude, :myLongitude), 'mi') < 10];

Location myLocation = Location.newInstance(10, 10);
Double myDistance = 100;

List accountList = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, :myLocation, 'mi') < :myDistance];

The key of these queries is in the WHERE clause. Querying based on the results of a query filtered by a formula to determine the distance between the specified point, provided by the javascript on your page, and the location of each record should provide the behavior you are looking for.


Below is an example of a page/controller which would be setup to perform these queries.

Page Example

<apex:actionFunction name="GetRecordsAtLocation" action="{!GetRecordsAtLocation}" rerender="ResultsPanel">
    <apex:param name="Longitude" assignTo="{!Longitude}" value="" />
    <apex:param name="Latitude" assignTo="{!Latitude}" value="" />
</apex:actionFunction>

<!-- ... -->

<script>
    var someGeolocation = something.GetBounds(); 

    GetRecordsAtLocation(someGeolocation.Longitude, someGeolocation.Latitude); 
</script>

As you can see, you'll need a way to pass the values from your page's javascript, over to the controller. This is where using an actionFunction comes in handy.

Controller Example

public List<Account> accountsAtLocation { get; set; }

public Double Longitude { get; set; }
public Double Latitude { get; set; }

public PageReference GetRecordsAtLocation() {

    accountsAtLocation = [SELECT Id, Name, BillingLatitude, BillingLongitude FROM Account WHERE DISTANCE(My_Location_Field__c, GEOLOCATION(:Latitude, :Longitude), 'mi') < 10];

    return null; 
}

You'll need a function setup to perform the query based on values set by the actionFunction, and then save those records to a variable which you can access on the page.

| improve this answer | |
0

Finally Google developer doc helped me. Here is the solution,

//Creating the map
var mapOptions = {center: myLatLng ,
                    zoom: 8,
                    mapTypeId: google.maps.MapTypeId.ROADMAP};
//Link with page component
var map = new google.maps.Map(document.getElementById("map_canvas"),  mapOptions);
//Get the bounds of the map this will return  LatLngBounds
var bounds = map.getBounds();
ver newGeolocation = new google.maps.LatLng(13, 81);
//Bounds will check whether given geolocation exist within the bounds
if(bounds.contains(newGeolocation) {
//Got the location which exist within the current map bounds
}

google.maps.LatLngBounds class has the inbuild function contains(latLng:LatLng|LatLngLiteral) Return Value: boolean Returns true if the given lat/lng is in this bounds.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.