Removing a polygon/circle around a marker? - google-maps-api-3

I hope you can help. I have a two pronged problem but I'm pretty new to javascript and the google maps API (v3) so please be gentle.
What i'm trying to do is add a marker to a map based on an address eg. Geocoding, then wherever that marker is, draw a circle around it at a radius of 5km. I've found some code to do the circle, and also the geocoding and both are working together nicely.
The problem is, I'm wanting to make the marker draggable to a new location and once at the new location, remove the old circle, and draw a new circle.
To understand what i'm trying to achieve, have a look at http://www.gleff.com/test/
With the above example, if you geocode an address, it centers on the marker location and draws the circle. I want to drag the marker somewhere else, and both remove the old circle, and draw the new circle based on the new location. At the moment, it just drags the marker to a new location but the circle stays where it is.
I assume the best approach is to drag the marker, then remove the overlay of the circle, and then re-add it at the new location.
Problem 1. How to remove the circle (overlay)
Problem 2. How to re-add it at the new location after dragging the marker
I've tried a number of things such as adding markers to an array eg. var markers = [];
and then trying to clear the overlays using this as an example.
http://code.google.com/apis/maps/documentation/javascript/examples/overlay-remove.html
I've been unsuccessful because I'm probably not putting the code in the correct place.
I've experimented with the overlay removal code in multiple parts of the javascript but nothing seems to work. Rather than go through all the locations of the code i've attempted to add the code to, i've purposely removed all reference to it to make it easier to understand.
So.. Can anyone offer any assistance? I just want to draw the circle around a marker, and then be able to relocate that marker (including the circle). But I want to understand how to remove the circle as well because ultimately, I will have multiple markers and will want to remove individual markers (including the circle) permanently from the map.
Note: I'm pretty sure I can work out how to remove the markers themselves, but the circle just doesn't want to be removed. If I geocode an address twice for example, the circle just draws a second time making it darker, rather than be removed from the map, and re-drawn.
Any assistance would be great.
Thanks
Geoff
Note: The only code i've modified is in the main.js

I'm not familiar with the API but you could have a look at what events are available in the API when you start dragging the marker and also when you drop the marker. Creating event listeners that draw and remove the circle around the marker is the way to go. I'm sorry I can't help you with WHAT event you need to listen for...

This gives a movable marker with circle overlay bound to it
function init() {
var mapCenter = new google.maps.LatLng(56, -4);
var map = new google.maps.Map(document.getElementById('map'), {
'zoom': 10,
'center': mapCenter,
'mapTypeId': google.maps.MapTypeId.ROADMAP
});
// Create a draggable marker which will later on be binded to a Circle overlay.
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(56, -4),
draggable: true,
title: 'Drag me!'
});
// Add a Circle overlay to the map.
var circle = new google.maps.Circle({
map: map,
radius: 5000// 5 km
});
//Bind circle to marker
circle.bindTo('center', marker, 'position');
}
// Register an event listener to fire when the page finishes loading.
google.maps.event.addDomListener(window, 'load', init);

Related

gmap3: Center an InfoWindow so it fits to map-bounds?

Have a huge problem, searching for weeks now. I have many markers and big InfoWindows per marker that most of time don't show up completely in my map-bounds. How to move the map automaticly and show these big InfoWindow exactly in the center? Further I think of showing it not only in the center (because the InfoWindows are more long than broad) but, say, in a lower position of center (centered, but near to bottom on the map-bounds). I hope you understand, what I mean.
Further, my InfowWindow even changed in size after I click on a link in it and get even bigger (o my god ;-), so I also have to handle this also.
You probably know that InfoWindow has an option disableAutoPan, but it's defaulted to false so that you should automatically see it whenever it's opened. If you want it in the center, then your best bet is in the click event before opening the InfoWindow, to center the map on the marker first. The autopan from opening should handle automatically and adjust the viewport to fit the InfoWindow from there as necessary.
//marker and map defined somewhere already
var iw = new google.maps.InfoWindow();
google.maps.event.addListener(marker, 'click', function(){
map.setCenter(marker.getPosition());
iw.setContent('your content');
iw.open(map,marker);
});

How can I have a circle overlay drawn, then change its position based on new marker click - avoiding multiple circles?

Im using google maps api v3 and have it working beautifully, on marker OR external link click I have the map zoom in on that particular marker and a circle being drawn at that particular marker. each time a 'click' occurs the following runs:
// DO CIRCLE...
//circle.setMap(null); // CLEARS CIRCLE
var circle = new google.maps.Circle({
map: map,
center: marker.position,
fillColor: '#0000FF',
fillOpacity: 0.2,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 1,
});
circle.setRadius(1609.344);
circle.bindTo('center', marker, 'position');
// END CIRCLE SECTION
I would like to just have ONE circle appear at any given time, so while it currently makes 4 different circles if you click 4 different markers, I would like to first REMOVE all circles, prior to drawing the new circle. I tried this but it just appears to disable the ability to draw circles alltogether.
circle.setMap(null);
How can I achieve that? If possible I'd prefer to have one circle drawn on for the first click, then just have it move position with it being bound to the last clicked marker. Or if that's not do-able I'd like to have it clear any circle overlays, then redraw new circle bound to the last clicked marker.
If you only want one circle ever, make it global (defined with "var" outside of any function) and either destroy it and recreate it or move it to the new location (bind its center to a new marker).
example

Google Maps API v3, Ground Overlay not positioning image tag correctly

I am experiencing a strange positioning issue when placing an overlay in my home city (Syracuse, NY). First the test map:
http://hotsdg.com/~downtown/map.html
This map uses the example code to place my image over newark. No problem here. The image is skewed and distorted of course, by placement occurs as expected.
The second map:
http://hotsdg.com/~downtown/map_two.html
This map uses the example code with coords adjusted to place the image over Syracuse, NY. At first glance the overlay appears to be missing. However, upon inspection I discovered that the element which is used for the overlay has it's top set to 94px (at the initial zoom), instead of 0px. Adjusting this manually brings the image into view.
Any thoughts as to what might be happening here, or more importantly, why it's happening?
A google.maps.LatLngBounds takes SW, NE LatLngs for the constructor.
Your working map does that (SW, NE):
var imageBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(40.716216, -74.213393),
new google.maps.LatLng(40.765641, -74.139235));
the not working one is (NW, SE):
var imageBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(43.054340, -76.159101),
new google.maps.LatLng(43.042504, -76.142601));

Update the coordinates and center map to marker

I have a Google Maps JavaScript v3 map on my website that allows me to drag the marker to get the coordinates for the given location. This works perfectly but I also want to enter the coordinates manually and then click on a link to update the marker. This works great but I can't center the map to the marker (make it to pan to the marker).
I have also added a select box with solid coordinates to quickly get to a specific location on the map. I have the same problem for this one to as for the first problem.
The problem is that when I manually enter the coordinates or choose a fixed location, the marker only moves few steps from the origin position or it doesn't move at all. When you try to grab the marker and move it you can't move it and the X is far away from the marker. After few attempts to move the marker the maps goes blank but the marker stands still.
You can see my solution and problems on jsFiddle. How can I fix my issue?
Thanks in advance.
function moveMarker() {
var lat = parseFloat(document.getElementById('marker-latitude').value);
var lng = parseFloat(document.getElementById('marker-longitude').value);
var newLatLng = new google.maps.LatLng(lat, lng);
marker.setPosition(newLatLng);
map.setCenter(newLatLng);
}
Does that work?

Is there a helper function / formula to keep the circle overlay constant while zoom in/out within Google Map?

I'm using GMap V3 API Circle to display the event location, if the more events occurs in that location, the size of the circle, i.e the radius would be bigger, in other words, the size of circle only relates to the event number occur in that location.
However, the radius of circle API is in meters on the Earth Surface, which means if I zoom in/out, the visual size of circle varies, so is there some function or formula to keep the circle size constant no matter which zoom level I'm in?
Thank you!
Ryan
I haven't seen any function to make the constant circle overlay on Google map. So the alternative solution which I am using that I have make the image of circle and then use the marker overlay with custom image on Google map. Following is the code of marker overlay.
var image = 'circle.png';
var beachMarker = new google.maps.Marker({
position: centerPoint,
map: map,
icon: image,
});

Resources