Select points within a country using google maps api - google-maps-api-3

I have several markers plotted in a world map using google maps API.
I would like to click in a country and then delete all the other markers and only see the points within this country border. For example, if I click in Brazil I just wanna see the markers inside Brazil.
I also would like to get a list of lat-lngs of the points inside the country after clicking in the country.
Does anybody knows how can I do that?
Thanks in advance!

Maybe the most efficient way is, if you can, to add to all your markers the attribute of the country code where the marker belong to.
When a user click on the map, pass the given lat/long to Geoname webservice (for example, you can also use Google Geocoding API) to obtain the country code. Then loop over your marker array and keep all the desired markers.
To get lat long from a click and get the country code from Geoname:
google.maps.event.addListener(map, 'click', function( event ){
getCountryCode(event.latLng.lat(),event.latLng.lng());
});
function getCountryCode(latitude, longitude) {
$.getJSON('http://ws.geonames.org/countryCode', {
lat: latitude,
lng: longitude,
type: 'JSON'
}, function(result) {
console.log(result.countryCode);
}
}
If you're not able to append the country code to the markers attributes, you will have to work with polygons of each country and search that a point is in a polygon.
If so, maybe this extension could help you: https://github.com/tparkin/Google-Maps-Point-in-Polygon
And for polygons of each country, use a simple geometry like this one: https://github.com/johan/world.geo.json/tree/master/countries

Related

Geocomplete cities without country information in gecomplete

am using Google Geocomplete to auto suggest my visitors when typing in citie
It works well, but however it suggests country next to the city. For example, if i start to type Chennai, it will suggest Chennai,TamilNadu,India. I need only Chennai, without the country.
Does somebody know how to tell Geocomplete to show only cities without the country?
Thanks
The code from here:
Only list the cities name Geocomplete.js or Google Location Autocomplete
var options = {
types: ['(cities)'],
componentRestrictions: {}
};
$("#source").geocomplete(options);

How to find nearby cities from a given city

HERE has a lot of endpoints but I am struggling to find one that will take city name or coordinates and return nearby cities within radius (limited to cities/towns only).
Check out the HERE Reverse Geocoder.
Use parameter level=city&mode=retrieveAreas
See the full doc at https://developer.here.com/documentation/geocoder/topics/resource-reverse-geocode.html
You didn't specify the API Product you are using, so I searched a solution for the JS HERE API.
Get Cities
Ref: here
Table 2. Filter by cities around a point location within a radius (all parameters need to be set).
This parameters are mandatory:
{ details: Integer, nearbyMax: Integer, radius: Integer, center: Double }
see more...

Using HeatmapLayer with data from FusionTablesLayer (Google Maps API)

How can I display a heatmap with the Google Maps API with data from a Fusion Table (Layer)?
With a FusionTablesLayer I can select and display the data (as markers).
layer = new google.maps.FusionTablesLayer({
query: {
select: '\'Geocodable address\'',
where: 'City=\''+city+'\' AND Category = \'companies\'',
from: 'sdfgfdgfsdfewrrtre34534543egffg'
},
heatmap: {
enabled: true
},
templateId: 0,
styleId: 1
});
However, I can't display the data as heatmap. I tried adding 'maxIntensity' and 'radius' as parameters but this did not work. I get a map like this one in this post.
Now, I'm looking to the HeatmapLayer but don't know how to retrieve the data as with the FusionTablesLayer. Somehow the 'query' parameter does not work in HeatmapLayer.
Therefore my question: How can I display a heatmap with the Google Maps API with data from a Fusion Table?
Sorry, there is nothing you can do.
Heatmaps created via a FusionTablesLayer did not have any configurable options.
To be able to use a HeatMapLayer with the data from a FusionTable you must download these data, and the data must include a location(a LatLng, not an address ).
When you didn't store these LatLng's on your own in a column you're not able to get them, the LatLng's created internally via geocoding are not accessible via download.

Getting marker data from a google map v3

I have a google map v3 with a number of markers, which have a "category" attribute. I also have a left side menu bar, from which a user can select the desired category. When he/she clicks on the selected category, all the markers are cleared from the map, and only markers with the selected category are loaded.
Now, I need some sort of function which can search through the map and get the loaded markers and their data. The data will then be dynamically loaded in the right side menu bar. You can see the page I am talking about at this LINK
Please, any help will be highly appreciated...
I know this isn't the answer you are hoping for, but there is no good way to query the map and ask for all of the markers. As you load the map and create the markers, you want to keep an array of all of your markers or keep an array by category and track your markers in multiple arrays. Keeping an array for each marker type will make it much easier to turn them on/off; that's what I do. Hope this helps -
One solution:
1) Create an array of markers. Every time you create a marker, add it to the array. Also assign a property to the marker of 'category' or something.
marker.category='foo';
2) When user clicks on the button, look through your markers array and test if each marker matches the category. If it does not, set the map to null.
if (markers[iterator].category!='foo'){
markers[iterator].setMap(null);
} else {markers[iterator].setMap(map);}

Google map api v3 markers exclude a city or certain bound

I am using google maps api v3. I have a database of markers with lat and long address that I display on a map and that works fine.
The map is displaying markers for entire state and now I want to know how can exclude markers in a certain city? Say I am displaying a map of Indiana and want to exclude Indianapolis.
I have a rough idea where I can select the points and some how check if they are between a certain range but could someone elaborate on this with a more specific example or is there a better approach to this?
What you need is something called Reverse Geocoding, which will give you result of street name, city name and country name, given longitude latitude example.
I will do these steps :
Iterate all the marker and "reverse geocoding" the marker lat-long.
Check city result of the "reverse geocoding".
If the City is not inside your accepted cities, remove the marker.

Resources