Ooops... Forgot to ask - you do have a global variable named 'map' do you that can be accessed by the function? If not then the listener will not be created - and you should see an error reported in your browser. Something like 'map is undefined'?
Martin. On 21 Sep, 21:58, Peter <floranfe...@gmail.com> 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 <warwo...@gmail.com> 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 <floranfe...@gmail.com> 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 google-maps-js-api...@googlegroups.com. To unsubscribe from this group, send email to google-maps-js-api-v3+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.