Search code examples
javascriptgoogle-mapsgoogle-maps-api-2

How to add go button and will open google map via my phone


function initializeMap() {
    if (GBrowserIsCompatible()) { 
        var map = new GMap2(document.getElementById("allmap")); 
        map.setCenter(new GLatLng(<?php echo $s['latitude']; ?>,<?php echo $s['longitude']; ?>), 16); 
        var latlng = new GLatLng(<?php echo $s['latitude']; ?>,<?php echo $s['longitude']; ?>); 
        map.addOverlay(new GMarker(latlng)); 
        map.openInfoWindow(map.getCenter(),document.createTextNode("<?php echo $s['name']; ?>"));
    }
}

I wan to add get me direction on footer


Solution

  • You can add a link and treat like a button, or use a button with an onclick function that open a new tab.

    That's easy but may be you wonder wich url you should use. You can use this url template: https://www.google.com/maps/@{lat},{long},{zoom}z And get the values of lattitude and longitude from the map center.

    Let's see an example:

    HTML:

    <div id="allmap"></div>
    <button id="gobutton">GO</button>
    

    Javascript:

    function initializeMap() {
        if (GBrowserIsCompatible()) { 
            var map = new GMap2(document.getElementById("allmap")); 
            map.setCenter(new GLatLng(<?php echo $s['latitude']; ?>,<?php echo $s['longitude']; ?>), 16); 
            var latlng = new GLatLng(<?php echo $s['latitude']; ?>,<?php echo $s['longitude']; ?>); 
            map.addOverlay(new GMarker(latlng)); 
            map.openInfoWindow(map.getCenter(),document.createTextNode("<?php echo $s['name']; ?>"));
    
            var button = document.getElementById("gobutton");
            button.onclick = function() {
                var center = map.getCenter();
                window.open('https://www.google.com/maps/@' + center.lat() + ',' + center.lng() + ',16z');
            }
        }
    }