开发者

Google Map - Take coordinates

开发者 https://www.devze.com 2022-12-08 15:33 出处:网络
I have google map on my site(php,apache,my开发者_JAVA技巧sql). here is code: <script type=\"text/javascript\">

I have google map on my site(php,apache,my开发者_JAVA技巧sql). 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


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消