javascript - How set the current position center on the map? -
i tried different approaches found on site , in web don't work me(
this code:
<h:panelgroup id="gmwrapper" style="background-color: red"> <p:gmap widgetvar="mainmap" center="49.835, 24.0083" zoom="15" id="maingooglemap" type="hybrid" style="width: 600px; height: 400px" model="#{userplaces.model}" styleclass="gmappstyle" onpointclick="handlepointclick(event)" /> <p:commandbutton onclick="somefunc()"></p:commandbutton> <script type="text/javascript"> function somefunc() { if (navigator.geolocation) { alert("start") navigator.geolocation .getcurrentposition(success); alert("successful!") var elem = document .getelementbyid("maingooglemap"); alert("element find") elem.setcenter(new google.maps.latlng(37.4419, -122.1419)); alert("good done!"); } else { error('geo location not supported'); currentlat = 0.0; currentlng = 0.0; } } var currentlat; var currentlng; function success(position) { currentlat = position.coords.latitude; currentlng = position.coords.longitude; } </script> </h:panelgroup>
javascript code works alert("element find")
also tried:
elem.setattribute("center", currentlat.tostring() + ", " + currentlng.tostring()); elem.setattribute("center", currentlat + ", " + currentlng); elem.setattribute("center", "37.4419, -122.1419");
, few other..
from google maps docs: panto(latlng:latlng|latlngliteral) - changes center of map given latlng. if change less both width , height of map, transition smoothly animated. below snippet creates map , centers map @ location.
<!doctype html> <html> <head> <title>simple map</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <style> html, body, #map-canvas { height: 100%; margin: 0px; padding: 0px } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> <script> var map; function initialize() { var mapoptions = { zoom: 8, center: new google.maps.latlng(-34.397, 150.644) }; map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); map.panto(new google.maps.latlng(37.4419, -122.1419)); } google.maps.event.adddomlistener(window, 'load', initialize); </script> </head> <body> <div id="map-canvas"></div> </body> </html>
Comments
Post a Comment