I have a page that allows the user to drag a marker and it
automatically updates a lat and lon textbox with the coordinates a
user drags them to. It also allows them to enter an address and it
retrieves the lat and lon of that address and updates the marker to
that point. The problem I am having is that the marker always stays in
the center. Meaning you can drag it somewhere, but as soon as you move
the map it resets the marker back to the center. Does anyone know what
I am doing wrong? Here is my code.
var map;
var geocoder;
globalLat = 40.7001;
globalLon = -99.0844;
function map_initialize() {
//if the browser is compatible with the gmap api
if (GBrowserIsCompatible()) {
//initialize the map
map = new GMap2(document.getElementById
('entityMap'));
//add the controls
map.addControl(new GLargeMapControl());
map.addControl(new GMenuMapTypeControl());
map.setCenter(new GLatLng(globalLat, globalLon),
15);
//enable the zooming
map.enableDoubleClickZoom();
map.enableScrollWheelZoom();
//add the marker and set the drag to true
globalMarker = new GMarker(map.getCenter(),
{ draggable: true });
//if they drag the marker
GEvent.addListener(globalMarker, 'dragend',
function(p) {
map.panTo(p);
} //function(p)
); //GEvent.addListener
//add the marker
map.addOverlay(globalMarker);
//add the geocoder
geocoder = new GClientGeocoder();
var templat = $find('<%= latInput.ClientID %>');
var templon = $find('<%= lonInput.ClientID %>');
//set the textboxes to the latitude and long
templat.set_value(map.getCenter().lat().toFixed
(6));
templon.set_value(map.getCenter().lng().toFixed
(6));
//link up the listeners
GEvent.addListener(map, 'moveend',
function() {
globalMarker.setLatLng(map.getCenter());
templat.set_value(map.getCenter().lat
().toFixed(6));
templon.set_value(map.getCenter().lng
().toFixed(6));
} //function
); //GEvent.addListener
} //if gbrowsercompatible
} //map_initialize
function map_geocode(address) {
geocoder.getLatLng(address,
function(p) {
if (p) {
map.panTo(p);
} //if (p)
else {
alert('Sorry, the geocoder failed to
locate the specified address :-(');
} //else
} //function
); //geocoder.getLatLng
} //map_geocode
GEvent.addDomListener(window, 'load', map_initialize);
//map_initialize(40.7001, -99.0844);
//this function will get the latitude and longitude
//for a given address and set a tool tip to visible
//if it can't find the address.
function getLatLon(sender, args) {
//we only do something if the over ride button
//is not checked
if (!(document.getElementById("overRide").checked)) {
var street = $find('<%= streetInput.ClientID
%>').get_value();
var city = $find('<%= cityInput.ClientID
%>').get_value();
var state = $find('<%= stateInput.ClientID
%>').get_value();
var zip = $find('<%= zipInput.ClientID
%>').get_value();
var address = (street + ", " + city + " " + zip);
geocodeAddress(address);
}//if ! .checked
} //getLatLon
//this function takes in an address and returns
//the lattitude and longitude.
function geocodeAddress(address) {
var coder = new GClientGeocoder();
coder.getLatLng(address,
function(point) {
//the id of the tool tip
var toolTip = $find("<%=
cantFindLocation.ClientID %>");
if (!point) {
//show the tool tip if it is
hidden
if (!toolTip.isVisible()) {
toolTip.show();
} //if
//the tool tip is showing and
the clicked the link
else {
toolTip.set_text("Still no
dice, <a href=\"#\" onclick=\"getLatLon()\">" +
"try again</
a> or get them <a href=\"" +
"http://
itouchmap.com/latlong.html\" target=\"_blank\"" +
">manually</
a>.");
} //else
} //if
//we set the textboxes to their
values
else {
//reset the global lat and long
//and call the function to
reload
//the map
globalLat = point.lat();
globalLon = point.lng()
map_initialize();
//hide the tooltip if its
showing
if (toolTip.isVisible()) {
toolTip.hide();
}
} //else
} //function(point)
);
} //geocodeAddress
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Maps API" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Maps-API?hl=en
-~----------~----~----~----~------~----~------~--~---