1

I am using this code to get the latitude and longitude of current location.

<script>
var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML="Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;    
  }
</script>

i need to pass the longitude and latitude from this ajax code to google map's LatLng variable how to this.

This is the google maps api code that i am using

    function initialize() {
  var latlng = new google.maps.LatLng(52.000,17.1100);
    var myOptions = {
      zoom: 12,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

Can anyone help me..thanks in advance :)

4

4 回答 4

0
function showPosition(position)
{
    x.innerHTML="Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude;
    initialize(position);
}

function initialize(position) {
    var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
    var myOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
于 2012-09-26T11:46:12.373 回答
0

如果您已经初始化了地图,您可以在获取位置后将其居中:

map.setCenter(new google.maps.LatLng(position.coords.latitude,position.coords.longitude));
于 2012-09-26T11:42:03.107 回答
0

以与您当前相同的方式,但传入position.coords.latitudeandposition.coords.longitude而不是52.000and 17.1100

var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
于 2012-09-26T11:37:54.900 回答
0

这应该只是传递您已经检索到的经纬度的情况:

function showPosition(position)
{
 var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
 var myOptions = {
  zoom: 12,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
 };

}
于 2012-09-26T11:38:16.250 回答