Google maps user editable polygon - google-maps-api-3

Using Google maps API V3, specifically using the drawing tools (only the rectangle is enabled) as seen here:
https://developers.google.com/maps/documentation/javascript/overlays#drawing_tools
But I have the following question,
Can I (if so how), by action of the user clicking a link on the same page, retrieve the lat,lng of the top left corner of the polygon and the bottom right, and send to a url, ie;
http://foo.bar/script.php?latoftopleft=17.4479216&longoftopleft=78.37720100000001&latofbotright=17.443172404867163&longofbotright=78.39395944192506

Retrieving the Rectangle
One way to retrieve the rectangle drawn by the user is to use Drawing Events:
var rectangle;
google.maps.event.addListener(drawingManager, 'rectanglecomplete', function(newRect) {
rectangle = newRect;
});
Whenever a rectangle is drawn by the user, it will assign the Rectangle overlay object to the rectangle global variable.
Extracting information about the Rectangle
The google.maps.Rectangle class has a getBounds() method, which returns a LatLngBounds object. You can use the getNorthEast() and getSouthWest() methods to deduce the top-left and bottom-right corner coordinates.
You can bind an onclick event to the link. The click event handler might look something like this:
function clickEventHandler(event) {
// check if the rectangle has been assigned
if (rectangle == undefined) {
alert('No rectangles have been drawn yet!');
return;
}
// obtain the bounds and corner coordinates of the rectangle
var rectangleBounds = rectangle.getBounds();
var northEast = rectangleBounds.getNorthEast();
var southWest = rectangleBounds.getSouthWest();
// deduce the top-left and bottom-right corner coordinates
var topLeft = new google.maps.LatLng(northEast.lat(), southWest.lng());
var bottomRight = new google.maps.LatLng(southWest.lat(), northEast.lng());
// Now that you have the coordinates, all you have to do is use an AJAX
// technique of your choice to send the HTTP request to script.php.
// ...
}
Further reading:
How do I bind a click to an anchor without a framework (javascript)
Pure Javascript send post data without a form

Related

How to get only visible markers?

Is possible to get only the visible markers? Is there any function for this?
This is the function where I have the bounds:
hereMap.addEventListener('mapviewchangeend', (evt) => {
var bounds = hereMap.getViewModel().getLookAtData().bounds;
}, false);
Can you please to know what visible marker actually implies ? however if you want to limit the visibility of markers, GeoCoordinates can be specified for a bounding box and passed in mapview, that can limit the visibility of map in the view. Also you can then set the visibility of inside markers to true.

how to change the marker icon when click on it here map

After loading The heremap i set markers with custom image on it. Then After click in the marker i'm getting the bubble popup. I want to change the marker icon when click on it. How can i change the marker icon when click on it?
Code for showing bubble after click the marker as given below.
map.addObject(group);
// add 'tap' event listener, that opens info bubble, to the group
group.addEventListener('tap', function (evt) {
// event target is the marker itself, group is a parent event target
// for all objects that it contains
var bubble = new H.ui.InfoBubble(evt.target.getPosition(), {
// read custom data
content: evt.target.getData()
});
// show info bubble
ui.addBubble(bubble);
}, false);
In my application I'm changing the original color of the marker when it is clicked. Have you tried using a markerTemplate, meaning a variable where you store the svg for your marker as a string?
var markerTemplate = '<svg xmlns="http://www.w3.org/2000/svg" width="28px" height="36px">... fill="${COLOR}"/></svg>'
You can then use e.g. fill="${COLOR}" in this string and in your EventListner replace this placeholder with a new color markerTemplate.replace('${COLOR}', 'rgba(231, 0, 0, 0.7)');
Hope this helps

Clicking a button places user's current location on map using Google Maps API V3

I understand how to get my current location with HTML5.
How do I add a click handler, so when a button is pressed it shows my current position on the map?
Google's example pretty much explains how it's done, but the button is set to predefined coordinates. How do I attach the "Home" button to current position coordinates instead?
Custom Control Example
From Google demo
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
As initialLocation is global you can use this to set the click event listeners: simply set the map to initialLocation
google.maps.event.addDomListener(controlUI, 'click', function() {
map.setCenter(initialLocation)
});

Google Maps API V3: fromContainerPixelToLatLng

I'm currently working on a project that requires that I have a div stacked above a Google Map. However, I need to pass the mousemove event of the div to the Map. To do that, I need to find the LatLng co-ordinates from the map container pixel co-ordinate (since triggering the Maps mousemove event requires the LatLng co-ordinates).
Is there any other way to pass the mousemove event from the div to the map, and if not, how do I go from the Map container co-ordinates to LatLng. I read that doing so requires creating a dummy overlay, and then using the getProjection() on that to get a MapCanvasProjection, and finally calling the fromContainerPixelToLatLng(). Is there any simpler way or do I really have to create a dummy overlay first?
As far as I can tell, this is the way you have to do it. I was reluctant at first, too, since it seemed like such overkill, but once I did it everything worked great. Here's an example implementation with a convenient delayedInit() callback:
function Dummy(map) {
this.setMap(map);
}
Dummy.prototype = new google.maps.OverlayView();
Dummy.prototype.draw = function() {
if (!this.ready) {
this.ready = true;
google.maps.event.trigger(this, 'ready');
}
}
Dummy.prototype.onAdd = function(){
// the Overlay dummy is ready and can be called upon
delayedInit();
}
var dum;
... and after you've instantiated your Google map:
dum = new Dummy(map);

Set a double click event without disabling default mouseup/mousedown behavior

I'm trying to enable a double click event on a flex control without disabling the default mouseup/mousedown behaviors.
I'm using the ESRI Flex API for arcgis server, and I have a map control with one background layer and a GraphicLayer. The graphics layer has several Graphic objects that respond to mouseover, and allow the user to pan the map if they click and hold. However, when I implement a double click event handler for the graphic objects, they no longer seem to bubble up their default behavior to the map.
Is there a way to implement a double click on a Graphic object while preserving the old behavior from clicking and holding?
I solved this by attaching the double click event to the map, rather than the graphic, and using the target attribute of the event to get the graphic I wanted to use.
Like this:
map.addEventListener(MouseEvent.DOUBLE_CLICK, function(event:MouseEvent):void
{
var graphic:Graphic = event.target as Graphic;
if(graphic)
{
...
}
});
If you set the "checkForMouseListeners" property to false on your Graphic objects, then the default map click/drag behavior will be preserved.
graphic.addEventListener(MouseEvent.DOUBLE_CLICK, function(event:MouseEvent):void {
var graphic:Graphic = event.target as Graphic;
if(graphic) {
...
}
});
//preserve the default click/drag behavior on the map
graphic.checkForMouseListeners = false;
http://resources.esri.com/help/9.3/ArcGISServer/apis/Flex/apiref/com/esri/ags/Graphic.html#checkForMouseListeners

Resources