I'm working with the Flash Api for Google Maps in my Actionscript/Air project. I now need the latLngBounds (the top left, top right, bottom right and bottom left) values of the current view of the map. The function map.getLatLngBounds should do that, but it always gives me a really low lat/lng bounds back.
Always somewhere around:
MapBounds{ lat: { hi: 0.8147270895329215 , low: 0.814011505887222 }, lng: { hi: 0.19547530117504353, low: 0.19447761445167258 } }
It's always between 1 and -1. It that how it should be? If it is, How do I use that LatLngBound to determine if a certain LatLng (for example { lat: 47.315383, lng: 12.750291 }) lies inside the boundary?
http://code.google.com/intl/nl-NL/apis/maps/documentation/flash/reference.html#IMap.getLatLngBounds
It's been a while since I've worked with Google Maps Flash API, so I don't remember as much as I'd like to be able to help with this question, but based on what you said, you can try creating a new LatLngBound with the getNorth() functions so that you are checking against the right values instead. It's merely a workaround, and I'm not even sure it will work.
Google map coordinates are not always intuitive though. You might want to look at some of the methods that translate coordinates. It's most likely returning what it thinks is correct based on the zoom level and viewport, but not what you would expect. I'll update this post later if I think of something else.
Related
When I'm searching within the SearchBox component
https://developers.google.com/maps/documentation/javascript/examples/places-searchbox
the official demo example (link above) doesn't work for me.
The map shows Sydney
I type in "mcdonalds" as an example
I select the first suggestion from the list (McDonald's) https://take.ms/cs03e
the map gets repositioned to my country (~ a lightyear away from Sydney) and displays the results only around my physical location.
The documentation about bounds
An options argument, which can contain the bounds property: bounds is a google.maps.LatLngBounds object specifying the area in which to search for places. The results are biased towards, but not restricted to, places contained within these bounds.
I tried bounds, location + radius, componentRestrictions to country, but none gave the expected pehavior.
I understand that the bounds are not always respected, although the way it works at my end is not usable at all.
We noticed the same malfunctioning within an existing app of ours that uses SearchBox which was working perfectly in the past. Bounds were respected.
Can anyone suggest a workaround or explanation for this behavior?
I tried alternatives such as
nearbySearch => respects bounds, but no option to define free text search
freetextSearch => only freetext search, no bounds
AutocompleteService.getPlacePredictions => respects bounds and freetext search but returns only ids of places and not locations (needs additional query for location to render markers)
PlacesService.findPlaceFromQuery => only returns one item
The goal is to search (freetext) for establishments and addresses within bounds and display them on the map as markers.
UPDATE:
Please see the inconsistent behavior from different IPs: https://take.ms/CQUDJ testing from most countries the bias is not working as it should.
Thanks
The issue is confirmed by Google, please see the related tickets.
https://issuetracker.google.com/issues/229752141 (mine)
https://issuetracker.google.com/issues/228928701
I'm working on understanding user events in Google Maps. Here's my sample code:
var listener = google.maps.event.addListener(map, 'rightclick', function(e){
console.log(JSON.stringify(e, null, 4));
});
The console returns:
{
"latLng": {
"lat": 36.29330392714158,
"lng": -115.24877548232325
},
"pixel": {
"x": 581.5555419921875,
"y": 374.7916578363487
},
"ca": {
"x": 46.04531521257013,
"y": 100.26925500235961
}
}
So I understand Latitude and Longitude.
The Pixel coordinates seems to represent the display on my webpage (with html canvas like output) When I move the cursor to the upper left corner of the visible map element in my web page, and right click, the console shows nearly 0,0 . Lower right hand corner matches to the pixel height / width of my display. So I can deduce that the Pixel feedback represents the display on my webpage.
I don't understand the context for a 'ca'? In fact what is a 'ca'? Anybody know? Is that an abbreviation for something? I can see that the 'ca' value barely changes when I right click on different areas of the visible map. I have no clue where 'ca' 0,0 ends up. How would you use a 'ca' element, what is it for? Anybody have an official reference? Many thanks.
The ca is an undocumented property of the object created by the closure compiler.
It will (or at least can) change with every release of the API so should not be used.
Related questions:
Google.Maps.Event settings - Va versus Xa
Google Places coordinates changing keys
Google Map Api Uncaught TypeError: Cannot read property '0' of null
Google Maps API V3 - only showing a blank map when using the geocoder
-Google maps - Weird geometry.location attributes (G, K)
Google maps api: what are xa and pa in get bounds?
item.geometry.location.kb & item.geometry.location.jb returning undefined
I'd like to drag an object into my Google Map (API V3) from outside the map.
After some research, I found this very helpful post and I tried to adapt it to my project.
The main idea is to drag a .png image on the map and when the mouse button is down, get the actual coordinates and place a marker at that lat / lng.
But I noticed there is a difference between the point you drag your image and the point where your marker is placed. The difference is around 10 / 15 pixels on the sample linked above, regardless the zoom level. At max zoom, it's not very important, but the more you unzoom, the more the gap is important.
Illustration of the gap :
On my website, I'm trying to drag the green marker from outside the map to the South of Leman Lake, near to Geneva : (we are just BEFORE the mouse up, this is still my .png image)
And when I mouse up, the "real" marker is put here :
(we are at 100 kms / 60 miles of the wanted place...)
So, why such a gap ? In the sample I linked above, the gap is less important, but it also exists. There are no error in my browser's console, and it doesn't seem to be a tricky CSS problem.
Do someone knows how to correct this problem ?
Thank you
The calculation of the marker-position is not exact.
the offset of the map also must be used inside the calculation(when the map is not placed at the top left corner of the page)
the anchor of the marker by default is the bottom-center , but the script simply takes the position provided via the event-argument, what may give different results, depending on the point inside the image where you grip it.
Fixed function:
$(document).ready(function() {
$("#draggable").draggable({helper: 'clone',
stop: function(e,ui) {
var mOffset=$($map.getDiv()).offset();
var point=new google.maps.Point(
ui.offset.left-mOffset.left+(ui.helper.width()/2),
ui.offset.top-mOffset.top+(ui.helper.height())
);
var ll=overlay.getProjection().fromContainerPixelToLatLng(point);
placeMarker(ll);
}
});
});
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)
What is the most effcient approach to check if there are any markers within a viewport?
A perfect solution wouldn't require checking all the markers one-by-one if it's contained by the viewport.
The best solution that I came up with is to
on the application launch, create an array with reference to markers - sorted by 1 coordinate, i.e. latitude
get viewport's bounds with GMap2.getBounds() (as paullb suggested)
take lower latitude of viewport boundaries and look for it in the array (fastest to achieve with binary search)
check if every following marker fits within the viewport (up to marker's latitude <= viewport's upper latitude).
Use GMap2.getBounds() to find the bounding box. The use GLatLngBounds.containsLatLng() to check each marker to see if it is visible.
Alternatively you could try and use the same approach with the Marker Cluster if the value of each cluster is stored in an easily accessible way. (haven't looked myself)