Google maps setMap() and map options - google-maps-api-3

I am new to Google maps api v3.
In the Google maps official tutorial, some of the sample code using
new google.maps.Marker({
map: map //Map option
});
or
new google.maps.Marker({
//some options here
}).setMap(map);
What are the difference between each other?

Off the top of head:
You can create markers and add them to a map at a later time e.g. after clicking some button using setMap()
You can have multiple maps on one page. You can selectively add markers to any one of them using setMap()
You can also remove markers selectively from a map using setMap(null)

assuming you mean google.maps.Marker both codes finally result in exactly the same.
The first code initializes the Marker with a map-property, while the second code initializes the marker without the map-property and sets the map-property of the already initialized marker.

Related

Remove invalidated marker

I have a H.map.Group with a bunch of markers.
If i need to remove a single marker i run:
this.markerGroup.removeObject(marker);
But sometimes in other part of my code i run:
this.markerGroup.removeAll();
So if after i run single marker remove method it throws me an error, because there is no such marker on markerGroup layer because all markers were removed previously.
How can i know the marker is valid and exists on a map so i can remove it correctly?
In Yandex Maps i have isValid method so i can validate marker before making some actions with one:
marker.isValid && markerGroup.remove(marker);
If isValid is true it means the marker has geometry and it is on a map, if false means the marker just exists in memory and not attached to the map.
What is equal functionality in Here maps?
H.map.Marker#getParentGroup() or H.map.Marker#getRootGroup() should work for that case.

How to use Google Maps API to determine bounds of GeoJSON MultiPolygon before adding to map?

I need to pan and zoom map using map.fitBounds() with a bounds object of a GeoJSON multipolygon without first adding it to the map.
I can determine a mapped feature's bounds by using map.data.addGeoson() to add geoJSON multipolygon to map, then grabbing feature w/ map.data.getFeatureById() and using processPoints() from this example https://developers.google.com/maps/documentation/javascript/examples/layer-data-dragndrop.
But, like I said, I want to pan and zoom to the multipolygon without first mapping it. I'm trying to frame markers contained within the multipolygon to provide user with more context without immediately pan/zoom to the marker bounds, which could be to a zoom level that does not provide a user enough context. See http://brookfieldlogisticsproperties.com/available-properties. Compare using select filters to drill into state of Utah vs. drilling through the map.
One approach would be to iterate over a GeoJSON MultiPolygon coordinates with the constructors for google.maps.Data.MultiPolygon, google.maps.Data.Polygon, etc. This seems rather unpleasant.
A Data-layer must not be drawn on a map.
map.data is a built-in Data-layer that will be drawn on a map, to create a Data-Layer without drawing anything simply use
new google.maps.Data()
So the workflow would be:
var
//create a map
map = new google.maps.Map(someNode,options),
//create bounds
bounds = new google.maps.LatLngBounds(),
//create data-layer
data = new google.maps.Data();
//add geoJson to the data-layer
data.addGeoJson(yourGeoJson);
//process the points via the linked function
processPoints(data.getFeatureById('idOfTheFeature').getGeometry(),
bounds.extend,
bounds);
//set the bounds of the map
map.fitBounds(bounds);

Google Maps Engine Image Layer Events

I'm am trying to use a maps engine layer in google maps api v3 and am running into issues with events and opacity (accepted enhancement) for raster layers.
The issue is that raster type data does not allow any events as far as I can tell.
var layer2 = new google.maps.visualization.MapsEngineLayer({
layerId: '15658084116283052074-13711557424617485464',
map: map,
clickable: true,
suppressInfoWindows: false
});
google.maps.event.addListener(layer2, 'click', function (event) {
alert('click');
});
Demo (jsbin) with vector maps engine layer and raster layer here. Is there something I'm missing or is it simply not implemented?
Documentation:Maps Engine Layers
Edit: I would like to get the pixel value of the raster.
The click event in a DMEL is fired when a feature is clicked. In Maps Engine, a feature is the same as defined in GeoJSON. That is: a single geometry, or table row in your data set.
As raster layers are not attached to a tabular data source, they don't contain individual features like points and shapes.
Raster layers are much like the base layer in the map however, so you can grab a click event at the map level. e.g.
google.maps.event.addListener(map, 'click', function (event) {
alert(event.latLng);
});
I'm not sure what your intention is beyond just a lat/lng pair though. If you wish to interrogate pixel data you may be better off putting the raw data into a table.

How to loop through loaded KML in maps api v3?

I need to get markers, overlays and etc, that were loaded from KML, but i don't understand how.
Here https://groups.google.com/forum/?fromgroups=#!topic/umapper/YCfHEWaCxMc is written that i can loop through KMLayer. but i can't!
I loading KML that way:
var nyLayer = new google.maps.KmlLayer("http://www.searcharoo.net/SearchKml/newyork.kml", { suppressInfoWindows: true, map: map });
Then i don't see anyway to find what objects inside. I tried to look in debugger what nyLayer contain inside, but nothing like objects array. Also tried this:
var test = nyLayer[0];
But test is undefined
You can't access the Placemarks in a KmlLayer other than by using a click listener.
You can access them if you use a third party parser like geoxml3 or geoxml-v3, but then you lose the advantage of the KmlLayer's tile based rendering, so you will see performance degradation for large numbers of objects.
Example which creates a dynamic sidebar using geoxml3

How can I display moving object in google maps api 3

I want to be able to show movement in a google maps just like in this example
http://www.labnol.org/internet/live-flight-tracking-google-maps/12308/
I am starting, and followed an example to load markers from mysql and put them in a map. But this is all static. If I keep track of a moving object in my database, how can I display them in real time?
Thank you
Assuming marker is your Marker instance, you should use some Ajax call to get new coordinates, depending how you return them, lets say your script will return an Object of lat and long and assign it to variable new_location. Now you need to change marker position to new coordinates:
function change_pos(new_location) {
var LatLong = new google.maps.LatLng(new_location.lat, new_location.long);
marker.setPosition(LatLong);
}
Just call this function everytime you got replay from Ajax.
And thats it.

Resources