clearing a kml from google maps - google-maps

I have a google map and I am wanting to show this KML file, I can load the KML but I haven't yet figured out how to clear it off the map. I load the KML on the map when the user click a check box, but whe they uncheck the box the KML wont go away. Here is the code that I am using to load the KML.
var isShowData = false;
//showEMSz(null);
var tileEMSz;
function showEMSz(obj) {
isShowData = !isShowData;
if (isShowData) {
tileEMSz = new google.maps.KmlLayer({
url: '/ELTSCAD.kml'
});
tileEMSz.setMap(map);
}
else
{
// remove Option
tileEMSz = null;
}
}

set the map-property of the layer to null:
if(tileEMSz){tileEMSz.setMap(null);}

Related

Removing Dynamically Created kmlLayer

I have an application that contains an embedded browser window.
I want to have my application create a kmlLayer. I can do this on the fly with the following:
function addKmlLayer(kmlURL) {
var kmlLayer = new google.maps.KmlLayer({ url: kmlURL });
kmlLayer.setMap(my_map);
}
I'm going to allow my users to add several kmlLayers.
I also know that I can remove the layer by using kmlLayer.setMap(null).
How do I find my previous kmlLayer to set it to null? Is there a way to identify the kmlLayer (ID, Name, etc...)?
One option is to keep a global reference to the KmlLayer
var kmlLayer = null;
function addKmlLayer(kmlURL) {
kmlLayer = new google.maps.KmlLayer({ url: kmlURL });
kmlLayer.setMap(my_map);
}
Then to hide it, this will work:
kmlLayer.setMap(null);
Might want to do:
if ((kmlLayer != null) && kmlLayer.setMap) kmlLayer.setMap(null);

Ignore hidden features returned by identifyTask

I am building an app using javascript, google maps v2 and ESRI 10.1.
I have a DynamicMapServiceLayer and a single layer in my ESRI map service. I dynamically show or hide features on the layer using the ESRI setLayerDefinitions function based on filter values selected by the user at runtime.
When the user clicks on the map I use the ESRI IdentifyTask object to find what the user clicked on. I want to show an infowindow for the feature the user clicked on. My code is sort of working but it opens infowindows for features that are filtered out (not visible) on the layer.
How can I check to see if the user clicked on a visible feature and stop opening infowindows for hidden features? Or how can I get IdentifyTask to stop including hidden features in the response object it returns?
This is my identifyParameters task invoke set up
// set the identify parameters
var identifyParameters = new esri.arcgis.gmaps.IdentifyParameters();
identifyParameters.geometry = latLng; // where the user clicked on the map
identifyParameters.tolerance = 3;
identifyParameters.layerIds = [OUTAGES_LAYER];
identifyParameters.layerOption = 'all';
identifyParameters.bounds = map.getBounds();
var mapSize = map.getSize();
identifyParameters.width = mapSize.width;
identifyParameters.height = mapSize.height;
// execute the identify operation
identifyTask.execute(identifyParameters, function(response, error) {
if (hasErrorOccurred(error)) return;
addResultToMap(response, latLng);
});
UPDATE
I have upgraded to Google maps v3. Now the identify parameters support passing layerdef information as follows below. For example I can limit the identify operation to those features where FISCAL_YEAR = 2014. My problem is solved.
function identify(evt) {
dynamicMap.getMapService().identify({
'geometry': evt.latLng,
'tolerance': 3,
'layerIds': [12],
'layerOption': 'all',
'layerDefs': {12 : 'FISCAL_YEAR = 2014'},
'bounds': map.getBounds(),
'width': map.getDiv().offsetWidth,
'height': map.getDiv().offsetHeight
}, function(results, err) {
if (err) {
alert(err.message + err.details.join('\n'));
} else {
addResultToMap(results, evt.latLng);
}
});
}

Sidebar on Google Maps reacting to map

I have a Google Map (API v3) populated with markers that indicate data from a database. I would like to add a sidebar that checks which markers are currently visible on the map and adds them to a sidebar, adjusting whenever map is moved. Is something like that possible?
I have a GitHub project which loads a KML file containing placemarks, puts them in the sidebar, and uses the Google Maps API to dim / brighten the locations when changing the map. The relevant portion is this snippet:
google.maps.event.addListener(mapInstance, 'bounds_changed', function() {
currentBounds = mapInstance.getBounds();
for (i = 0; i < parser.docs[0].placemarks.length; i++) {
var myLi = $("#p" + i);
if (currentBounds.contains(parser.docs[0].placemarks[i].marker.getPosition())) {
myLi.css("color","#000000");
} else {
myLi.css("color","#CCCCCC");
}
}
});
The abbreviated version of that is:
google.maps.event.addListener(mapInstance, 'bounds_changed', function() {
currentBounds = mapInstance.getBounds();
/*
Loop over Google map placemarks and check the placemark by doing:
if (currentBounds.contains(currentPlacemark.getPosition())) {
// Change CSS to make sidebar entry visible
} else {
// Change CSS to make sidebar entry invisible
}
*/
});
In my examples, mapInstance is an instance of google.maps.Map, and currentPlacemark is an instance of one of your google.maps.Marker objects.

How to let user point their position in my Google maps application?

I want let user mark their position in our google maps application and then save it to our database, then it can be showed in our google maps application in the next time.
If you want to save the position where the user clicked you can use a "click" listener to get the latitude and longitude of the click with code like this then send it to your server using an Ajax style call where it can be stored in the database.
var clickListener = GEvent.addListener(map,"click",
function (overlay,latlng,overlaylatlng) {
alert(latlng);
});
This code is for v2 of the Google Maps API. Version 3 should have something similar.
An options is to try geolocation first and let the browser pinpoint them. If the browser doesn't support geolocation or an error occurs you can fall back onto them manually adding their position.
var geolocation = null; // holds the latlng object
navigator.geolocation.getCurrentPosition( geolocation_success_init, geolocation_error_init, {enableHighAccuracy: false, timeout: 10000} );
function geolocation_success_init( position ) {
geolocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
initialize_map();
}
function geolocation_error_init( error ){
initialize_map();
}
when you create your map check for geolocation
if ( geolocation ) {
marker = new google.maps.Marker({
position: geolocation,
map:.map,
title: "Your Location"
});
}

How to get markers after calling drive directions in Google Maps API?

I just started working using Google Maps API yesterday, and trying to set up drive directions to my map. My problem is: when I call the function load,
// [...]
gdir = new GDirections(map, directionsPanel);
// [...]
gdir.load("from: " + fromAddress + " to: " + toAddress);
it returns a map whose markers are not draggable. So, I need to make them draggable in order to recalculate the directions, but I can't get the markers objects.
Someone knows how can I do it?
You need to add a handler on the GDirections object for the addoverlay event:
GEvent.addListener(gdir, "addoverlay", onGDirectionsAddOverlay);
When your onGDirectionsAddOverlay handler is called you can iterate through the new markers and replace them with draggable copies:
for (var i = 0; i <= gdir.getNumRoutes(); i++)
{
var originalMarker = gdir.getMarker(i);
latLngs[i] = originalMarker.getLatLng();
icons[i] = originalMarker.getIcon();
newMarkers[i] = new GMarker(latLngs[i], { icon: icons[i], draggable: true, title: 'Kan flyttes' });
map.addOverlay(newMarkers[i]);
// add stuff to your newMarkers[i] drag end event...
// ...
//Bind 'click' event to original markers 'click' event
copyClick(newMarkers[i], originalMarker);
// Now we can remove the original marker safely
map.removeOverlay(originalMarker);
}
You can find a working example of this here (source).