Setting perfect zoom level for a Place with Google API - google-maps

In my Google Maps app the user can search for a place (using Autocomplete) and the map will be re-centered upon it with map.setCenter(pos);.
This does not change zoom level though, that must be done manually with map.setZoom(x).
In Google's map, zoom automatically readjusts to perfectly show the place's boundaries, how can I replicate that behavior?

The duplicate #woelliJ found almost worked, but ended up using an old version of the API. It led me on the right track though, and I ended up finding how to make it work with the new API - map.fitBounds(autocomplete.getPlace().geometry.viewport);

Related

Google maps set heading not working and map remains on heading to north

I am trying to do: map.setHeading(180) but this doesn't work and map remains heading to north. Everything else is working - I am using the very basic template of Gmaps from their docs, zoomings, pannings, markers and other things work as excepted, but the heading not. What am I missing in here?
For example, I got the position and heading from the GPS and I move the marker using marker.setPosition(position) which also works as excepted.
Tilt and rotation are now supported programmatically and by user input when using WebGL rendering. Please see https://developers.google.com/maps/documentation/javascript/webgl/tilt-rotation for documentation and examples.
Try beta version of JavaScript API. add v=beta in url.
The reason for this is that you can use setHeading if your map is displaying 45 degrees imagery as stated on the documentation.
It looks like your use case is the same as this existing Feature Request which is requesting for a feature to change the orientation of the map. You can star the issue to receive updates regarding the issue. You can also leave a comment on the issue regarding your use case.

Google map zoom parameter in url not working

Why doesn't google map z (zoom) parameter work inside the url?
I' ve searched over so I got few solutions for sending zoom parameter in google map url but based on the old posts (stackoverflow) which deal with the zoom level within the url it seems that this functionalities do not work anymore.
I've used the following z parameter but it's not working:
http://maps.google.com/?q=38.6531004,-90.243462&z=12
Below is the link where they have mentioned z parameter for zoom level
link
For those who do not want to read,
this works (z range:[1-20]):
https://maps.google.com/?q=38.6531004,-90.243462&ll=38.6531004,-90.243462&z=3
There is no documentation that covers this problem. However, there are few hacks.
The link that you have provided,
URL (stackoverflow):What parameters should I use in a Google Maps URL to go to a lat-lon?
Will not help you with getting the desired google maps zoom level inside a url
The following examples which have been working for quite a while DO NOT WORK anymore (they work but without getting the desired zoom level)
Examples:
http://maps.google.com/?q=38.6531004,-90.243462&z=3
http://maps.google.com/?q=38.6531004,-90.243462&zoom=3
http://maps.google.com/maps?q=Scottish+Rite+Hamilton+ON&loc:43.25911+-79.879494&z=15
Instead, you can get the zoom level easily by trying the following examples:
https://www.google.com/maps/#38.6531004,-90.243462,9z
https://maps.google.com/?ll=40.7060471,-74.0088901&z=3
(Note: after clicking on google maps you will get a gray marker which appears and quickly disappears)
However the above URL's will not give you a static pinpoint for the specified lat&long.
There is a "hack" on how to get a fixed pinpoint (red marker pin) and that is by giving the q attribute (represents the search query) and the II attribute(latitude and longitude of the map centre point) along with the z (zoom attribute)
Example (with the OP coordinates):
https://maps.google.com/?q=38.6531004,-90.243462&ll=38.6531004,-90.243462&z=3
If you want to change the zoom level just change the value of 'z'.
After searching the web I found that only this website:
https://moz.com/blog/new-google-maps-url-parameters
deals with google maps parameters/attributes/arguments, where things actually still work. If anyone want's more functionalities within the url, then this is the site to check.
Zoom intervals explained (https://developers.google.com/maps/documentation/static-maps/intro#Zoomlevels):
The following list shows the approximate level of detail you can
expect to see at each zoom level:
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings
This works as of 5 Aug 2018
http://www.google.com/maps/place/<lat>,<lng>/#<lat>,<lng>,<zoom>z
For example
http://www.google.com/maps/place/49.46800006494457,17.11514008755796/#49.46800006494457,17.11514008755796,7z
As of 04/2020 ...."because google is always changing and evolving"
In complement to Eugen Sunic comments above, and an alternative update to the Solution.
When Logged in to google and getting map data from the google generator, it is not the same as copying the URL from the browser window (which my may be manipulated to load different zooms trough the various alterations, html, Css and script tricks).
But in the below, keep in mind that Your address info may need to be part of the google array of database, eg you have some address map info linked to your account, images, business name, etc. or not... maybe just typed in for a quick map search with a pin-point... the later I don't know.
Also the below is not some Google API pay per 1k clicks thing, which is more geared to the commercial development end of google tracking heat maps, etc. So if you just need a simple solution and certain zoom and marker to work..then you might try the below.
When logged in too your Google account (maybe not) and showing your location marker on the map screen...
Choose the zoom detail of the map you want to appear in your web page. eg Whole US, State views, City, Street (by clicking the + or - zoom feature).
Then go to the hamburger menu to the left, and look down the list for "Share or Embed Map" and the popup window should load the same zoom level of the map! Do not try to change the zoom in the popup window feature, it wont work to change the link code but will change the pop up windows zoom of the map. In short, the embedded link code is preset and will not change in the popup.
Above this popup window of the map there will be two options at the top (Share and Embed) button/link selections.
Click on the Embed link/button and it should generate the code for the zoom you previously chose.
Lastly, copy the code, paste it into your web page and TEST!
As far as I can tell, There is no altering this code as the others suggested above, changing(z=3, 9z, etc) as the code is entirely different. But it will allow you to get the zoom you need "if the google code generator or procedure does not change".
But the zoom level was altered and good enough for me when I figured the process out. Changing it to other zooms is another can of code.

Ushahidi - How to make the markers stay on the map on zoom change?

I am using the platform Ushahidi Web-2.7.3 , see: http://ti5.net.br/provedorlegal.com.br, and when I zoom in beyond a certain level, the clustered markers disappear from the map. I also tested this on an older version of a site, see: http://movimentofichalimpa.org/mapa, where the clustered markers do not disappear on zooming in, but just become ungrouped, as is normal with a cluster strategy. How can I make the markers remain on the map when zooming in?
Ushahidi actually uses OpenLayers under the hood -- the images may come from Google, but the Javascript library is pure OpenLayers. These markers are actually generated by adding what is known as a context to the style attribute of the OpenLayers.Layer.Vector and an associated Cluster.Strategy. There is a good example of it working properly here: OpenLayers cluster example and if you look at the Javascript source you will see how it is done: view-source:http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/strategy-cluster-threshold.html by defining a radius in the context of the styleMap.
The reason that your example isn't working is that there is actually a script error being triggered, which is stopping the clustering/styling from being performed. The styles are defined in the file ushahidi.js. If you open a Javascript debugger you will see the error for yourself that is firing before the ushahidi.js functions are called to determine the number of features in the cluster and therefore the radius of the markers.
The actual error is http://ti5.net.br/provedorlegal.com.br/index.php/json/cluster?s=1401591600&e=1404183599&z=9, but I have no idea why, as the OpenLayers is the minified version which is very hard to debug. You might find moving your application to OpenLayers directly will help, as it will be easier to debug, and it also works on mobiles, which I know is one of the reasons people like to use Ushahidi.

Can the google map type be changed to RoadMap when zoomed out?

Google has recently made changes to their maps, it switches to Terrain mode when zoomed out (from zoom level 17 onwards). Is it possible to switch it back or force it to remain in RoadMap mode using the javascript API?
You can not do it. Because Google changed tiles itself, so we cannot do it anything.
Kasheftin has come up to a solution to this as an answer to his question "Auto switch to other mapType on max zoom level in google maps": https://stackoverflow.com/a/18147423/348485

Google maps one placemark instead of many when zooming out

I have a google map with several placemarks, when you zoom out on it at some point several placemarks that are close, show as one, in the domain of the app this is a serious problem.
Is there anyway to show some sort of count or make obvious the fact that there are more than one placemark?
Cheers
MarkerClusterer was recently released on the GMaps Utility Library. It's the fastest client-side clusterer I've seen so far. There's a great example here which shows some of the options you can tweak.
This page compares and benchmarks some of the options available to you, and concludes that MarkerClusterer is the fastest.
Just keep in mind that if you have several thousand markers, any client-side solutions will probably be too slow.
There's an extension by Martin Pearman that addresses this situation:
ClusterMarker detects any groups of two or more markers whose icons visually intersect when displayed. Each group of intersecting markers is then replaced with a single cluster marker. The cluster marker, when clicked, simply centres and zooms the map in on the markers whose icons previously intersected.
There seems to be a problem with the redirection on Martin's page at the moment, but if you keep reloading it eventually works.