Reputation: 3521
I can already create placemarks on click! What I want is to prevent the user from creating another placemark with the same lat and long values. Here is my initial code based on Google Earth Api.
Somehow it doesn't seem to work...How do I make sure that the user wont create a new placemark on the same lat long?
I thought if (event.getTarget().getType() != 'KmlPlacemark' && event.getTarget().getGeometry().getType() != 'KmlPoint'
should do the trick.. Any idea? T_T
google.earth.addEventListener(ge.getGlobe(), 'click', function(event) {
if (event.getTarget().getType() != 'KmlPlacemark' &&
event.getTarget().getGeometry().getType() != 'KmlPoint') {
event.preventDefault();
//create a place marker for the pole
var poleMarker = ge.createPlacemark('');
var point = ge.createPoint('');
point.setLatitude(event.getLatitude());
point.setLongitude(event.getLongitude());
poleMarker.setGeometry(point);
ge.getFeatures().appendChild(poleMarker);
}
});
Upvotes: 1
Views: 933
Reputation: 17039
The logic of your anonymous function is a little redundant. Let me explain.
Firstly, you specify to listen for 'click' events on the target 'GEGlobe' object.
google.earth.addEventListener(ge.getGlobe(), 'click', ...
Then, in you conditional statement you are testing if the target of the event, the 'GEGlobe' object, is not a KmlPlacemark or a KmlPoint - But this is always going to be true. THis because of the way event propagation works. The event is always going to propagate to the GEGlobe and thus the condition will always be true.
if (event.getTarget().getType() != 'KmlPlacemark' &&
event.getTarget().getGeometry().getType() != 'KmlPoint') ...
You could look at event.stopPropagation
ala event.preventDefault
but for your case a simple solution "...to prevent the user from creating another placemark with the same lat and long values..." would be to store the lat lng values, and not create a placemark if the values already stored. For example something like the following might work for you. Obviously there are other ways to do this, but the principal of storing the locations and then checking against them is going to hold true however you actually code it.
// to hold the places clicked
var locations = new Array();
google.earth.addEventListener(ge.getGlobe(), 'click', function(event)
{
event.preventDefault();
// create a string of the place
var place = event.getLatitude() + ',' + event.getLongitude();
// if the place is not the locations array
if(locations.indexOf(place) == -1)
{
// add the place to the locations array
locations.push(place);
// create a place marker for the pole
var poleMarker = ge.createPlacemark('');
var point = ge.createPoint('');
point.setLatitude(event.getLatitude());
point.setLongitude(event.getLongitude());
poleMarker.setGeometry(point);
ge.getFeatures().appendChild(poleMarker);
}
});
Upvotes: 1