Extend Bound after direction service google map api v3 - google-maps

I used markers as well as direction service in my map.
I used bounds.extend(lat),map.fitBounds(bounds);to extend the maps to show all my markers. This was working fine until i used direction service. once the direction is used, map automatically zooms to the direction from A to B.Only two markers pointing A and B are visible now in map.
But i want the map to show all the markers with the direction used between two other markers.
How could i do it?
Issue solved by using the following line
var directionsDisplay = new google.maps.DirectionsRenderer({
preserveViewport:true
});

If you don't want the directionsRenderer to zoom to its results, use the preserveViewport option.
Another option would be to use the union of the bounds of the route returned by the directionsService and the bounds you calculated for your markers.

Related

google map zoom after directions

I am using google maps in order to give some directions from one point to another.
Although, I have set the zoom of the map to 15 the zoom changes after the response of the direction request in order probably to fit the directions into the map.
Does anyone nows how to keep the zoom constant at 15. and focus at the first point?
See the documentation for the DirectionsRenderer
preserveViewport: true
will prevent the DirectionsRenderer from changing the zoom.
To center the map on the first point use the map.setCenter function. You will need to parse the response from the directions service and create a google.maps.LatLng object for the first point.
This example shows one way to parse the response:
http://www.geocodezip.com/v3_directions_custom_iconsC.html
(you don't need everything, just the location of the point you want to center on)

How to find the boundary points with center and zoom level given?

The LatLngBounds function needs the corner points!
How to find the boundary points with center and zoom level given?
Is there Google API V3 which achieves this?
Or can this be done someway with the static maps?
Are you referring to the boundary points of the visible map? If so, you can use the getBounds method of the map object.
var llb = map.getBounds();
If you are using the geocoder, it returns a suggested viewort (that is a google.maps.LatLngBounds() object).
See this similar question:
Google Maps API zoom after setcenter
If you only have the center coordinates and the zoom, use them to initialize the map, listen for the bounds_changed event, then use the map.getBounds() function.
All of the above are described in the documentation

Get zoom and center to fit all markers in a google static map

I'm working in a Java Me application that uses google static maps. The problem is that I need to get the zoom and center that fits all markers, and I need to add some labels in some coordinates on the map. How can I do this? I need a function in the server or in the midlet to get this parameters, I can't use javascript.
You don't need to calculate it on your own.
When you supply markers, usually the map will have a viewport to show all markers.
To modify the viewport use the visible-parameter.
Referring to the comments:
Pseudo-Code for calculating the center from a couple of Points:
latitude: (maxLatOfAllPoints+minLatOfAllPoints)/2
longitude:(maxLngOfAllPoints+minLngOfAllPoints)/2
I have had the same problem. Don't use "&zoom=xx" in your url, the map will auto-fit for all your markers!
Define bounds object
bounds = new google.maps.LatLngBounds();
When you create and place marker on the map extend bondes with marker position.
bounds.extend(mPosition);
where mPosition is a google new google.maps.LatLng(0,0) object, the same that you use during marker creation.
at the end ot you can create a link for click or just run.
map.fitBounds(bounds);
That is it.

google maps api v3: top and left pixel position of marker

I want to retrieve the top and left position of marker (in pixels) in google map api v3. How can this be done? I know that there are some hints on using fromLatLngToDivPixel(), but may I know how it works? example?
You mean the coordinates in pixels within the div? Look at http://qfox.nl/notes/116
overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map);
var point = overlay.getProjection().fromLatLngToContainerPixel(latLng);
or
var point = overlay.getProjection().fromLatLngToDivPixel(latLng);
Ugly indeed. Much easier in v2, where these methods were directly methods of GMap2 object. Another flaw of google api v3!
If you facing problem having the overlay undefined, that's because the object is created after the map is idle.
Better call this within event listener, full explanation here https://stackoverflow.com/a/6657723/4026345

Google Maps API: setCenter from markers possible?

I have set of markers on Google map.
Is it possible to center the map in the way that all markers will be visible, and zoom level will also be auto adjusted?
Exactly the same way how Goolge Static Maps API does when you specify markers and do not specify center/zoom parameters (Implicit Positioning of the Map).
You can use var myBounds = google.maps.LatLngBounds() and basically extend all the points you have. Once you have all the points in the LatLngBounds() you can use your map.fitBounds(myBounds); and all marker should be visible and centered.
do you wish me to prototype a jsfiddle?