The alert displaying 'hi 2' should appear only after you have clicked the map.
Clicked the map that is - look at the documentation: http://code.google.com/apis/maps/documentation/javascript/reference.html#Map Scroll down to the Map events and look at the 'click' event: "This event is fired when the user clicks on the map (but not when they click on a marker or infowindow)." Are you clicking the map or a marker or a infowindow? Martin. On 21 Sep, 21:58, Peter <[email protected]> wrote: > Ok, so now I have: > <form id="myFormId"> > <input type="text" id="lat" name="lat" /> > <input type="text" id="lng" name="lng" /><br /> > <a href="javascript:void(0);" > onclick="javascript:getMapClick();">Enable map click</a> <br /> > <div id="map_canvas" style="width:300px; height:300px"></ > div> > </form> > > and javascript in external .js file: > > function getMapClick(){ > // get the LatLng of the map click and complete the search > form's address box with that value to 4 decimal places > alert('hi 1'); > var listener = google.maps.event.addListener(map, 'click', > function (clickEvent) { > alert('hi 2'); > var myForm = document.forms.myFormId, latLng = > clickEvent.latLng, lat = latLng.lat(), lng = latLng.lng(); > myForm.lat.value = lat; > myForm.lng.value = lng; > // remove the event listener > google.maps.event.removeListener(listener); > listener = null; > }); > > }; > > when I click on the 'Enable map click' link, the first 'hi 1' is > displayed, but the 'hi 2' not...what is going wrong? > > On Sep 21, 10:28 pm, Martin <[email protected]> wrote: > > > Hi. > > > Here's some code i've used recently that you should be able to modify > > to your needs: > > > // map must be global > > // and assume you have a form with an id 'myFormId' which has two text > > inputs with names 'lat' and 'lng' > > function getMapClick(){ > > // get the LatLng of the map click and complete the search > > form's > > address box with that value to 4 decimal places > > var listener=google.maps.event.addListener(map, 'click', > > function(clickEvent){ > > var myForm=document.forms.myFormId, > > latLng=clickEvent.latLng, > > lat=latLng.lat(), lng=latLng.lng(); > > myForm.lat.value=lat; > > myForm.lng.value=lng; > > // remove the event listener > > google.maps.event.removeListener(listener); > > listener=null; > > }); > > > }; > > > You could use a button or anchor to call the function: > > > <a href="javascript:void(0)" onclick=getMapClick()">Enable map click</ > > a> > > > Martin. > > > On 21 Sep, 12:46, Peter <[email protected]> wrote: > > > > I want users to be able to click on a worldmap. Then, when theyu click > > > I get the longitude and latitude of the clicked location and store > > > them in two textboxes... > > > Where can I find how to do that? -- You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" 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-js-api-v3?hl=en.
