How to draw A LOT of polygons on google maps api v3 - google-maps-api-3

I'm trying to create a map that highlights over 2 dozen regions on a google map via coordinates. It seems i'm hitting the query string limit.
Any suggestions as to a work around?
Thanks in advance for all/any recommendations.
Thank you for the comments. I lost track of this post and didn't get a chance to test out your solutions.
The image attached below is what I was trying to fill out.
My polygons are region groupings and i'm trying to color code them.
{
where: "Members in("+arrGrpOne+")",
polygonOptions:{
fillColor: '#FF0000',
fillOpacity : .5
}
},{
where: "Members in ("+arrGrpTwo+")",
polygonOptions:{
fillColor: '#FF00FF',
fillOpacity : .5
}
If i add more where clause i get the following error :
Failed to load resource: the server responded with a status of 400 (Bad Request)
https://mts0.googleapis.com/mapslt/ft?hl=en-US&lyrs=ft%3A115hPQ0BVPioObq47a…vwwtvt,twtuuwvwwtvv&z=12&src=apiv3&xc=1&callback=xdc._d9mcoj&token=25665

Depending on how complex your polygons are you may be able to use encoded polylines:
https://developers.google.com/maps/documentation/staticmaps/?csw=1#EncodedPolylines
Try and recreate your polygon with the interactive encoder.
GitHub should have several libraries available depending on your application.

Related

Quality of result set

I use the here PlacesServices to retrieve information what's around me. Often I get results that are quite ambiguous / duplicate because outdated data appears to be in the result set that reduces the quality quite significantly. How can we feed back changes to the community or how and when does here get updates for those categories, e.g. restaurants or petrol-stations?
Is there a way to dedup?
This is a good example for 3/6 duplicates (same petrol station) since the chain changed some time ago dependent of the direction on the highway.
https://places.ls.hereapi.com/places/v1/places/276u1jne-8c62ebd6159c441eba290df4efdcfd1d;context=Zmxvdy1pZD02ZWQ0YzViNS0wMzgxLTUxZDAtOTg2ZC00NjQ3YTVjNWJhYTJfMTU3NTY2NjU1MTYwM18wXzE1NCZyYW5rPTI
https://places.ls.hereapi.com/places/v1/places/276u1jne-3c240a265c6e48d698a400b7d8738202;context=Zmxvdy1pZD02ZWQ0YzViNS0wMzgxLTUxZDAtOTg2ZC00NjQ3YTVjNWJhYTJfMTU3NTY2NjU1MTYwM18wXzE1NCZyYW5rPTQ
3 chains for a single petrol station
Finally, is there a solution how I'd only obtain those in my travel direction?
var query = {"in": lat +"," + lng +";r="+distance*1000,"cat" : categories +",pretty"};
let entryPoint = H.service.PlacesService.EntryPoint;
await this.places.request(entryPoint.EXPLORE, query,
function(response) {
values = response.results.items;
}, function(resp) {
console.log('ERROR: '+resp);
});
Best regards and many thanks in advance
O.
In general the feedback related to HERE Map data can be reported by either the Map Feedback API , the details on the API are available on the documentation page , or The Online Tool HERE Map Creator could be used.
With respect to retrieving POIs, it is possible to request POIs along the route using the "browse/by-corridor" (documentation) end point where the route shape and a radius could be provided as a corridor. It should be noted however the API does not consider the heading direction of the route and may return POIs on the other side of a road. Example :
https://places.ls.hereapi.com/places/v1/browse/by-corridor?route=[52.5199356,13.3866272|52.5100899,13.2816896|52.4351807,13.1935196|52.4107285,13.1964502|52.38871,13.1557798|52.3727798,13.1491003|52.3737488,13.1154604|52.3875198,13.0872202|52.4029388,13.0706196|52.4105797,13.0755529]&apiKey=API_KEY

How to export here maps to image file for printing programmatically?

Just like leaflet and Arcgis JS Api have support for printing and exporting map to image files, how to do it in here maps? I explored here API and searched web but found nothing.
You have to take a look at our Map Image REST API.
https://developer.here.com/documentation/map-image/topics/what-is.html
What Is the Map Image API?
The HERE Map Image API is a REST API that allows you to request static map images for all regions in the world. The map images show conventional map views, but can also include points of interest, routes (for example, with turning points and junction views), statistics and heat maps.
In addition, the API offers a variety of supplementary services for displaying location-based data. For example, it is possible to present roadsigns.
You can also request the map images in different formats:
0 PNG
1 JPEG (default)
2 GIF
3 BMP
4 PNG8
-5 SVG (only for companylogo)
If this is not given, JPEG is used as default.
Within JavaScript, you can configure a MapTileService to request tiles of the map.
https://developer.here.com/documentation/maps/topics_api/h-service-maptileservice.html
There is a capturing functionality in HERE javascript API.
I made a quick draft how you can easily export the captured canvas element of the map with anything rendered on top of it:
// overlay element containing captured canvas element
var captureBackground = document.createElement('div'),
bgStyle = captureBackground.style;
bgStyle.width='100%';
bgStyle.position='absolute';
bgStyle.top='0';
bgStyle.bottom='0';
bgStyle.background='rgba(0,0,0,0.7)';
bgStyle.padding='30px';
bgStyle.zIndex=1000;
captureBackground.addEventListener('click', function(e) {
document.body.removeChild(this);
});
// capture the map:
map.capture(function(capturedCanvas) {
// remove previously added canvas from the overlay
captureBackground.innerHTML = '';
captureBackground.appendChild(capturedCanvas);
document.body.appendChild(captureBackground);
}, [], 50, 50, 700, 700);
For more information see https://developer.here.com/documentation/maps/topics_api/h-map.html#h-map__capture

Errors When Calculating Distance Between Two Addresses

I have the following script which is being used in a spreadsheet to calculate the driving distance between two cities or a city and a zip code of another city. It is being run for approximately 25 locations simultaneously. To better explain, I have cell B3 in which I enter a new city every time. The script is then used in cells adjacent to my 25 plant locations to calculate the distance from each of my plants to the variable city.
It uses google sheets built in mapping api and works on 80% of the calculations but returns "TypeError: Can Not Read Property "legs" from undefined. (line 16). The plants that it fails on vary with every new city so its not like it is for certain locations. It is almost like the api times out before it completes some of them. I split it into two separate scripts with a varied name and that worked for a day but then 20% fail again.
To make things slightly more odd, I have another script that sorts the plants based on closest distance to the variable address. When you sort the plants, even the ones with errors go to their correct location based on distance. So it is like the distance script is obtaining the correct disance but displaying the error anyways.
Clear as mud? Would love any input I could get on how to correct the issue or an alternate mapping api that could solve my problems.
function distancecalcone(origin,destination) {
var directions = Maps.newDirectionFinder()
//Set the Method of Transporation. The available "modes" are WALKING, DRIVING, BICYCLING, TRANSIT.
.setMode(Maps.DirectionFinder.Mode.DRIVING)
//Set the Orgin
.setOrigin(origin)
//Set the Destination
.setDestination(destination)
//Retrieve the Distance
.getDirections();
return directions.routes[0].legs[0].distance.value/1609.34;
}
Have you tried using a try-catch block around directions.routes[0].legs[0].distance.value ?
try{
return directions.routes[0].legs[0].distance.value/1609.34;
}
catch (e){
console.log("error",e)
}
or you could try something like this
alert(directions);
alert(directions.routes[0]);
alert(directions.routes[0].legs[0]);
alert(directions.routes[0].legs[0].distance);
alert(directions.routes[0].legs[0].distance.value);
and so on...to find out which one comes up as undefined the first. That might help you to debug the issue.
Enable Direction Api
1)Go to "google cloud platform"
2)go to "Api and services"
3)search for "direction api" and enable it
The directions service is subject to a quota and a rate limit. Check the return status before parsing the result.
For lots of distances (or at least more than 10), look at the DistanceMatrix.
I'm able to run the script from the Script editor, but not from spreadsheet. The error is "unable to read property legs" when the function is called from spreadsheet. But the property is in place when called from Script editor and contain correct values.
You probably need to use WEB API and have API KEY:
Google Apps Script - How to get driving distance from Maps for two points in spreadsheet

Dynamically set bounds on nearbySearch based on search critieria

We've implemented a search box and google maps on our page to allow customers to perform searches based on places queries, and so far it's working well. However, using TextSearch we almost always get 20 results (unless it's a specific point). What we prefer though, is to return a set of results that makes more sense to a user based on their search (i.e. if they're searching for churches within a zipcode, we shouldn't show churches outside the zip code).
I know we can bias our results based on location and radius, and even restrict results based on location / radius using NearbySearch.
However, our customers are national users who may be searching in any area in the world, so we're not sure, until the user searches, what location and radius to set as a restriction. I'd like to determine that dynamically based on their query.
For example, in Google Maps if you search for "Churches near 30319" you get a much more localized result set than "Churches near Georgia"
Churches near 30319:
https://maps.google.com/maps?q=churches+near+30319&hl=en&sll=33.772251,-84.296934&sspn=0.049158,0.082312&hq=churches&hnear=Atlanta,+Georgia+30319&t=m&z=14
Churches near Georgia:
https://maps.google.com/maps?q=churches+near+Georgia&hl=en&sll=33.870438,-84.332304&sspn=0.049102,0.082312&hq=churches&hnear=Georgia&t=m&z=8
I've tested doing a separate query using geocode to get the single-point location of the query. i.e.
getGeneralVicinity = ->
address = $('#address').val()
window.oneq.geocode.geocoder.geocode
address: address,
(results, status) ->
if status is google.maps.GeocoderStatus.OK
console.log(results[0].types)
It seems by possibly finding the type of the geocode result (i.e locality) we could determine a radius and use the geometry.location for the location bounds. Unfortunately, it's not consistent, and if a user only searches for "churches", this doesn't give us the desired results.
Any help would be greatly appreciated.
You almost had it. I will refer to the Geocoding API as its JSON feed rather than the Google implementation of it, so detail will come straight from the source. There are some very interesting parameters that come back when geocoding something. Try it:
Georgia: http://maps.googleapis.com/maps/api/geocode/json?address=georgia&sensor=true
30319:http://maps.googleapis.com/maps/api/geocode/json?address=30319&sensor=true
Both of the second-tier address components are Georgia. However, there are a few differing parameters, and the one you want is geometry. This indicates the shape of the area.
Take, for example, the 30319 request. You will get as bounds:
"bounds" : {
"northeast" : {
"lat" : 33.9203610,
"lng" : -84.30943599999999
},
"southwest" : {
"lat" : 33.83286890,
"lng" : -84.35826589999999
}
},
"location" : {
"lat" : 33.87309460,
"lng" : -84.33842899999999
},
This tells you three things:
The corners of the bounding box
The centre of the box (which will be the intersection of the vertex lines)
This allows you to compute the maximum distance from your centre, which you can then feed back into your google places API search as radius. Conversion from lat/long to distance is trivial: it's called the orthodromic path. Two formulas exist - one for small distances (Haversine's formula), the other for large distances (this). Someone wrote a calculator for these: http://williams.best.vwh.net/gccalc.htm . You'll quickly see that the bounding box for 30319 spans 10km, whilst the georgia one spans almost 700 (which would require multiple Google Places requests to match).
Let me know if this wasn't clear and I'll elaborate further.

Draw line between given coordinates using Google Map API [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
polyline snap to road using google maps api v3
My usecase is very common but somehow not finding right solution. I keep recording the coordinates of a moving vehicle and then display the predefined route along with the traveled path. The predefined route and traveled path should be displayed in different color. For displaying predefined path I am using DirectionsService.route. And for traveled path I tried different approaches and none of them worked perfectly.
Draw a Polyline between all the coordinates recorded in path traveled. But in case turn falls between 2 recorded coordinates then the line directly joins the points ignoring the turn.
var line = new google.maps.Polyline({
path: arr_all_coordinates, strokeColor: "red", strokeOpacity: 1, strokeWeight: 1
});
Use new DirectionsRenderer objects to form a route between 2 consecutive coordinates and display with DirectionsService.route till the last coordinate. In this case map keep jumping to the last 2 coordinates and gives focus to the last route drawn. I tried setting preserveViewport to true but didnt work.
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsRenderer.setDirections(response);
}
Any suggestion please.
This is basically the same problem as drawing a line that snaps to roads. See my solution for that problem: polyline snap to road using google maps api v3

Resources