Hi.. The map variable needs to be a reference to the google.maps.Map object and it needs to be accessed from within the getMapClick() function.
Do you create your google.maps.Map object in a function? If so then once that function has executed there is no reference to the Map left. Typically you'd declare the 'map' variable at the start of your code, outside of any functions: // create a global variable var map: Then in the function where you create your map, instead of defining the 'map' variable like this: var map=new google.maps.Map(blah blah); The 'map' variable is already defined so you'd do this: map=new google.maps.Map(blah blah); You've then created a global variable 'map' and defined it's value (the google.maps.Map object) in a function. The 'map' variable is now accessible from anywhere in your code - including the getMapClick() function. Martin. On 22 Sep, 09:43, Peter <[email protected]> wrote: > Problem is that the site is not live yet... > But my question is more: what should I put in this map variable? Is it > a reference to the map_canvas? > If so, would this suffice: var map = > document.getElementById('map_canvas'); ? > > On Sep 22, 1:42 am, Martin <[email protected]> wrote: > > > Post a link to your map - that's the only way me or someone else can > > suggest some code to fix it. > > > Martin. > > > On 21 Sep, 23:46, Peter <[email protected]> wrote: > > > > Indeed, didnt see that the first time, but indeed I get js error: "map > > > is not defined" > > > So what should I do about that? > > > > On Sep 21, 11:27 pm, Martin <[email protected]> wrote: > > > > > 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 <[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.
