Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-17-2009, 11:13 AM   PM User | #1
lostprophetsie
New to the CF scene

 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
lostprophetsie is an unknown quantity at this point
Google Maps Latitude and Longitude

Hi I'm currently using some code that takes the latitude and longitude from a Google map and outputs it in a table when the user drags a marker on the map. I previously had it working that the outputted latitude and longitude appeared in an input box but I can no longer get this to work again.

Any help is greatly appreciated. I have posted the Javascript code below.

Code:
<script type="text/javascript">
 
 function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var center = new GLatLng(55.54555, -3.39634 );
        map.setCenter(center, 15);
        geocoder = new GClientGeocoder();
        var marker = new GMarker(center, {draggable: true}); 
        map.addOverlay(marker);
        document.getElementById("lat").innerHTML = center.lat().toFixed(5);
        document.getElementById("lng").innerHTML = center.lng().toFixed(5);
 
      GEvent.addListener(marker, "dragend", function() {
       var point = marker.getPoint();
          map.panTo(point);
       document.getElementById("lat").innerHTML = point.lat().toFixed(5);
       document.getElementById("lng").innerHTML = point.lng().toFixed(5);
 
        });
 
 
     GEvent.addListener(map, "moveend", function() {
          map.clearOverlays();
    var center = map.getCenter();
          var marker = new GMarker(center, {draggable: true});
          map.addOverlay(marker);
          document.getElementById("lat").innerHTML = center.lat().toFixed(5);
       document.getElementById("lng").innerHTML = center.lng().toFixed(5);
 
 
     GEvent.addListener(marker, "dragend", function() {
      var point =marker.getPoint();
         map.panTo(point);
      document.getElementById("lat").innerHTML = point.lat().toFixed(5);
         document.getElementById("lng").innerHTML = point.lng().toFixed(5);
 
        });
 
        });
 
      }
    }
 
       function showAddress(address) {
       var map = new GMap2(document.getElementById("map"));
       map.addControl(new GSmallMapControl());
       map.addControl(new GMapTypeControl());
       if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
          document.getElementById("lat").innerHTML = point.lat().toFixed(5);
       document.getElementById("lng").innerHTML = point.lng().toFixed(5);
         map.clearOverlays()
            map.setCenter(point, 14);
   var marker = new GMarker(point, {draggable: true}); 
         map.addOverlay(marker);
 
        GEvent.addListener(marker, "dragend", function() {
      var pt = marker.getPoint();
         map.panTo(pt);
      document.getElementById("lat").innerHTML = pt.lat().toFixed(5);
         document.getElementById("lng").innerHTML = pt.lng().toFixed(5);
        });
 
 
     GEvent.addListener(map, "moveend", function() {
          map.clearOverlays();
    var center = map.getCenter();
          var marker = new GMarker(center, {draggable: true});
          map.addOverlay(marker);
          document.getElementById("lat").innerHTML = center.lat().toFixed(5);
       document.getElementById("lng").innerHTML = center.lng().toFixed(5);
 
     GEvent.addListener(marker, "dragend", function() {
     var pt = marker.getPoint();
        map.panTo(pt);
    document.getElementById("lat").innerHTML = pt.lat().toFixed(5);
       document.getElementById("lng").innerHTML = pt.lng().toFixed(5);
        });
 
        });
 
            }
          }
        );
      }
    }
    </script>
This is the table I output the latitude and longitude to

Code:
<table class="latlontable" style="float:left">
  <tr>
    <td width="100"><b>Latitude</b></td>
    <td id="lat" name="lat"></td>
  </tr>
  <tr>
    <td width="100"><b>Longitude</b></td>
    <td id="lng" name="lng"></td>
  </tr>
</table>

Last edited by lostprophetsie; 06-18-2009 at 12:31 AM..
lostprophetsie is offline   Reply With Quote
Old 06-17-2009, 07:11 PM   PM User | #2
lostprophetsie
New to the CF scene

 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
lostprophetsie is an unknown quantity at this point
I'm still searching for a solution for this so if anyone has any ideas please let me know.
lostprophetsie is offline   Reply With Quote
Old 06-17-2009, 11:33 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If that code works, then I can't see why you can't just do:
Code:
<form name="LatLngForm">
Latitude: <input name="lat">
Longitude: <input name="lng">
</form>
and then change
Code:
      document.getElementById("lat").innerHTML = point.lat().toFixed(5);
      document.getElementById("lng").innerHTML = point.lng().toFixed(5);
to
Code:
      document.LatLngForm.lat.value = point.lat().toFixed(5);
      document.LatLngForm.lng.value = point.lng().toFixed(5);
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
lostprophetsie (06-18-2009)
Old 06-17-2009, 11:47 PM   PM User | #4
lostprophetsie
New to the CF scene

 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
lostprophetsie is an unknown quantity at this point
Hi Old Pedant,

I tried implementing your solution, changing my javascript but the values are not appearing in the input boxes?

Thanks
Tommy
lostprophetsie is offline   Reply With Quote
Old 06-18-2009, 12:30 AM   PM User | #5
lostprophetsie
New to the CF scene

 
Join Date: Jun 2009
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
lostprophetsie is an unknown quantity at this point
Hi Old Pedant,

Just had another look and your code was perfect thank you so much for the help.

Tommy.
lostprophetsie is offline   Reply With Quote
Old 06-18-2009, 12:32 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,178
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Phew. Good.
Old Pedant is online now   Reply With Quote
Old 06-24-2009, 08:27 AM   PM User | #7
shaggy
New to the CF scene

 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
shaggy is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
If that code works, then I can't see why you can't just do:
Code:
<form name="LatLngForm">
Latitude: <input name="lat">
Longitude: <input name="lng">
</form>
and then change
Code:
      document.getElementById("lat").innerHTML = point.lat().toFixed(5);
      document.getElementById("lng").innerHTML = point.lng().toFixed(5);
to
Code:
      document.LatLngForm.lat.value = point.lat().toFixed(5);
      document.LatLngForm.lng.value = point.lng().toFixed(5);

I am having the same problem (Cannot get the form inputs to accept the lat and lng.

Did you change all three occurrances of
Code:
document.getElementById("lat").innerHTML = point.lat().toFixed(5);
to
Code:
document.LatLngForm.lat.value = point.lat().toFixed(5);
as well as the three occurances of the
Code:
document.getElementById("lng").innerHTML = point.lng().toFixed(5);
to
Code:
document.LatLngForm.lng.value = point.lng().toFixed(5);
When I did that the google map disappeared.

Thanks
shaggy is offline   Reply With Quote
Old 06-24-2009, 09:20 AM   PM User | #8
shaggy
New to the CF scene

 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
shaggy is an unknown quantity at this point
Never mind... I figured it out. All the references to .innerHTML needed to be replaced by the corresponding version of document.LatLngForm.lat.value or document.LatLngForm.lng.value. Once this was done everything works great. Thanks.
shaggy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:41 PM.


Advertisement
Log in to turn off these ads.