0
<body onload="initialize()">
    <form id="form1" runat="server">
    <div id="map_canvas" style = "width:320px; height:480px">
    </div>
    <div>
        <asp:TextBox ID="address" runat="server"></asp:TextBox>
        <asp:Button Text="find" runat="server" OnClientClick="codeAddress()" />
    </div>
    </form>
</body>

and this is my javascript:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
        var geocoder;
        var map;
        function initialize() {
            geocoder = new google.maps.Geocoder();
            var latlng = new google.maps.LatLng(-34.397, 150.644);
            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        }
        function codeAddress() {
            var address = document.getElementById("<%= address.ClientID %>").value;
            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var marker = new google.maps.Marker({
                        map: map,
                        position: results[0].geometry.location
                    });
                } else {
                    alert("Geocode was not successful for the following reason: " + status);
                }
            });
        }
    </script>

the codeAddress() function is not working at all ! what's the problem?

alex
  • 1
  • 3
  • What do you mean with `not working at all`? Have you set a breakpoint in this function? ([IE-Developer-Toolbar](http://www.microsoft.com/downloads/en/details.aspx?FamilyID=95e06cbe-4940-4218-b75d-b8856fced535) / [Firebug](https://addons.mozilla.org/de/firefox/addon/firebug/)) – Tim Schmelter May 18 '11 at 22:57
  • yes, it's not hitting the function ! – alex May 18 '11 at 22:58
  • now it says `Unable to get value of the property 'offsetWidth': object is null or undefined` – alex May 18 '11 at 23:03
  • @Tim it's skipping the `geocoder.geocode()` in the `codeAddress` function – alex May 18 '11 at 23:19
  • @alex: i have no experiences with google's geocoding api and i don't see any obvious error or typo. But maybe [here](http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/v2/services.html#Geocoding) or [here](http://code.google.com/intl/en-us/apis/maps/documentation/javascript/services.html#Geocoding) is something interesting. Edit: i see, the second link is where you have your code from ;) – Tim Schmelter May 18 '11 at 23:26
  • got the mistake, since it's an `` there is a post-back happening, and every time on post-back, the `initialize()` function is being called since it's in the `onload()` of the ``.. so it appeared as though it wasn't working, whereas, it was initializing it every time on button click .. sorry for the trouble caused – alex May 18 '11 at 23:31
  • But in [this example](http://code.google.com/intl/en-us/apis/maps/documentation/javascript/services.html#GeocodingStatusCodes) the address-Textbox has the value `value="Sydney, NSW"`. Edit: Glad it works now. – Tim Schmelter May 18 '11 at 23:34
  • @Tim big problem now, I've written huge logic in my code-behind for the `findButton_click()`, now if I make it an `` to avoid the postBack issue as discussed previously, how do I make it use the `findButton_click()` logic in the codeBehind? – alex May 18 '11 at 23:45
  • @alex: i'm not sure if i understood you correctly. I thought this button is for the `codeAddress`-function only and therefore clientside. Then add another button for your `findButton`-serverside-logic. Edit: if you need to know if the `codeAddress`-function returns `google.maps.GeocoderStatus.OK` to postback, you could check it. If its <> `google.maps.GeocoderStatus.OK` you have to `return false` from your onClientClick handler. That prevents the postback. – Tim Schmelter May 18 '11 at 23:52
  • @Tim there's just 1 button, I enter the place I want to search and hit `find`, I'm calling the `codeAddress`function `onClientClick`, this part is fine, the problem arises when, the `codeAddress()` executes and does a PostBack(since it's an asp:button), because of the postback the `initialize()` is called, and hence I can't see the result of `codeAddress()`, so I can do 2 things, change asp:button to html button, or move the initialize() to somewhere else – alex May 18 '11 at 23:57

1 Answers1

0

Try...

document.getElementById("<%= address.ClientID %>")

Change to:

document.getElementById("address").value;
Chains
  • 12,541
  • 8
  • 45
  • 62
  • it's an asp control, so I have to bind it, else the value will change at run-time – alex May 18 '11 at 23:47
  • Really? (asp, not asp.net?) Are you using master pages or something? Otherwise, you might set the "clientID" property on the control, because that is different that the ID property. – Chains May 19 '11 at 00:11