rendom
rendom

Reputation: 3705

Google Map - Take coordinates

I have google map on my site(php,apache,mysql). here is code:

<script type="text/javascript">
        function initialize() {
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map_canvas"));
            var point = new GLatLng(<? $default_ll ?>,<? $default_spn ?>);
            map.setCenter(point, 15);
            map.setUIToDefault();
            map.setMapType(G_NORMAL_MAP);
            var marker = new GMarker(point);
            map.addOverlay(marker); 
        }
    }

    window.onload = initialize;
    window.onunload = GUnload;
    </script>
    <div id="map_canvas" style="width:500px;height:300px;"></div>

I want to make button(outside of map div), so when user chose hes coords and clicks this button. Current location he is watching rightnow will be put in to database. Well for simplicity can you show how to assign current: ll to $new_ll and spn to $new_spn

Upvotes: 0

Views: 1101

Answers (1)

tcarobruce
tcarobruce

Reputation: 3838

The GMap2 map object has these methods:

var center = map.getCenter();   // returns GLatLng of center point of map
var span = map.getBounds();   // returns GLatLngBounds of current map view

Using these simply you could have a button like this:

<input type="button" onclick="javascript:alert(map.getCenter())" value="Show Coordinates"/>

More realistically, you'll define a function to be called by the button's onclick:

function showCenterAndSpan()
{
    var center = map.getCenter();
    var span = map.getBounds();
    var ne = span.getNorthEast();
    var sw = span.getSouthWest();
    var coordStr = "lat=" + center.lat() + "&lng=" + center.lng() + 
        "&n=" + ne.lat() + "&e=" + ne.lng() + "&s=" + sw.lat() + "&w=" + sw.lng();
    alert(coordStr);   // Or, post this string to the server to save into the database
}

Upvotes: 2

Related Questions