views:

97

answers:

4

Hi All. I tried with the following but it doesn't work

var txtlat=document.getElementById('TextBox1').value=GMap1.getCenter().lat();
var txtlong=document.getElementById('TextBox2').value=GMap1.getCenter().lng();

It gives a JavaScript error as "Object doesn't support this property or method".

How can i do this????

I really need help...

Thank you!

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication5._Default" %>

<%@ Register assembly="GMaps" namespace="Subgurim.Controles" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <cc1:GMap ID="GMap1" runat="server" 
            Key="ABQIAAAAs98ZVKM_IHFkRP_EavW_DhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxQGoS16N7wYnBPhgtjTxMaUVN58kA" />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

    </div>
    <script type="text/javascript">

    var txtlat=document.getElementById('TextBox1').value=GMap1.getCenter().lat();
    var txtlong=document.getElementById('TextBox2').value=GMap1.getCenter().lng();

    </script>
    </form>
</body>
</html>
+1  A: 

Hello,

Did you confirm the value returned from getCenter is not null? It should be a LatLng object, but maybe it's returning null... are you sure the center has been set yet? Also, what object type is GMap1? How do you create that object?

Brian
Hello Brian,I'm sorry.i'm new to this google maps environment.I can't understand exactly what u are telling.Gmap1 is the ID of the Gmap controller.I still haven't resolved this.Thank you!
chamara
See @Daniel's sample, do you conform to that setup of the GMap control?
Brian
+2  A: 

2nd UPDATE: Further to the updated question, you also have another problem with the server-side textbox controls. They cannot be referenced from JavaScript using document.getElementById() as you are doing.

You may either use normal HTML controls: <input type="text" id="textbox1" /> or else you would have to use something like document.getElementById('<%= TextBox1.ClientID %>') to reference the textbox from JavaScript.


1st UPDATE: Further to the comments, the examples below have been updated to return the lat/lng of where the mouse is clicked.


You may want to check out the following examples, which have been tested to work correctly:

Using the v3 API:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps getCenter()</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>
  <input type="text" id="textbox1" />
  <input type="text" id="textbox2" />

  <script type="text/javascript">
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      center: new google.maps.LatLng(35.55, -25.75),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    google.maps.event.addListener(map, 'click', function(event) {
      if (event.latLng) {
        document.getElementById('textbox1').value = event.latLng.lat();
        document.getElementById('textbox2').value = event.latLng.lng();
      }
    });

  </script>
</body>
</html>

Using the v2 API:

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps getCenter()</title> 
    <script src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;sensor=false"
            type="text/javascript"></script> 
  </head> 
  <body onunload="GUnload()"> 
    <div id="map" style="width: 500px; height: 400px;"></div> 
    <input type="text" id="textbox1" />
    <input type="text" id="textbox2" />

    <script type="text/javascript"> 
      var map = new GMap2(document.getElementById("map"));
      var centerPoint = new GLatLng(35.55, -25.75);
      map.setCenter(centerPoint, 2);

      GEvent.addListener(map,"click", function(overlay, latlng) {     
        if (latlng) { 
          document.getElementById('textbox1').value = latlng.lat();
          document.getElementById('textbox2').value = latlng.lng();
        }
      });
    </script> 
  </body> 
</html>

Screenshot from the above examples:

Google Maps getCenter()

Daniel Vassallo
chamara
@chamara: Updated the examples to give the lat/lng of the point where the mouse is clicked... As to the `<script>` tag, I don't have experience using the ASP.NET controls for Google Maps, but I guess what you are doing is correct if you get the map working on your web page.
Daniel Vassallo
A: 

Daniel's answer works very fine.So does that mean i can't use the server side control

to extract the latitude and longitude in to textboxes??????? I'm at the middle of my project,stuck in this matter and i have done many server side codings using Gmap1 so i can not remove this server side control.I appreciate if you guys have any ideas to help me out. Thank you!

chamara
@chamara: You may want to check http://en.googlemaps.subgurim.net/ejemplos/ejemplo_96500_Eventos.aspx, and try to adapt this example to the V2 API example that I gave.
Daniel Vassallo
BTW... you may want to delete this answer, and edit the question to provide this information :)
Daniel Vassallo
A: 

Hey! Guys i found the answer forgot to mention here so the answer was very simple

Msg1 and Msg2 are DIV tags

protected string Gmap1_Click(object s, Subgurim.Controles.GAjaxServerEventArgs e)
        {
         return "document.getElementById('Msg1').innerHTML="
+ e.point.lat.ToString() + ";" +    "document.getElementById('Msg2').innerHTML="
+ e.point.lng.ToString();

        }
chamara