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?
Related
In app which is using google map js v3 I want to make a function that when tapping around the maker then map was opposite of dragging to that direction, for example 10 meters dragging to left when tapping to right.
in my case by adding value to the lat lng and map.setCenter() method moving map under marker
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));
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);
I have, for example, 10 markers on Google Map. But I zoomed and in visible area left only 2 markers.
Can Google Maps API somehow return me a number of markers in visible area (2 for my example)?
You can evaluate map.getBounds().contains(marker.getPosition()) with each marker to see which ones are included in the current viewport.
The above code is not working. There is no method as such getBounds(). Below is the updated code :
VisibleRegion visibleRegion = map.getProjection().getVisibleRegion();
LatLngBounds mapBound = visibleRegion.latLngBounds;
if(mapBound.contains(marker.getPosition()){
// do somethings i.e. such as changing marker icon and others.
}
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,
});