2012-08-01 37 views
1

我能够得到我自己的经度和纬度,并将它们保存到瓦尔:谷歌地图API:使用我自己的COORDS在JavaScript API

var myLat = position.coords.latitude; 
var myLong = position.coords.longitude; 

我只是不知道如何将它们传递给代码这带来了地图的屏幕上的位置INTIAL:

function initialize() { 
        var mapOptions = { 
         center: new google.maps.LatLng(-34.397, 150.644), 
         zoom: 4, 
         mapTypeId: google.maps.MapTypeId.ROADMAP 
        }; 
        var map = new google.maps.Map(document.getElementById("map_canvas"), 
                mapOptions); 
       } 

我需要myLat和myLong送入线:

center: new google.maps.LatLng(-34.397, 150.644) 

我只是不知道该怎么去做。任何人都可以将我指向正确的方向吗?

编辑:添加完整的代码为每个请求如下

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=YOUR KEY HERE&sensor=true"></script> 

    <script type="text/javascript"> 

     //MAP 
     function initialize() { 
      var mapOptions = { 
       center: new google.maps.LatLng(-34.397, 150.644), 
       zoom: 9, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      map = new google.maps.Map(document.getElementById("map_canvas"), 
            mapOptions); 
     } 

     //GEOLOCATION 
    var onSuccess = function(position) { 
     alert('Latitude: ' + position.coords.latitude + '\n' + 
       'Longitude: ' + position.coords.longitude + '\n'); 

     var myLat = position.coords.latitude; 
     var myLong = position.coords.longitude; 

     map.setCenter(new google.maps.LatLng(myLat, myLong)) 


    }; 

    // onError Callback receives a PositionError object 
    // 
    function onError(error) { 
     alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
    } 

    navigator.geolocation.getCurrentPosition(onSuccess, onError); 

</script> 

和HTML:

<body onload="initialize()"> 

<div id="map_canvas" style="width:300px; height:400px;"></div> 

</body 

>

+0

为什么你不能只是'new google.maps.LatLng(myLat,myLng)'? – 2012-08-01 05:09:38

回答

3

之所以这样是不工作是怎么一回事,因为下面的函数

var onSuccess = function(position) { 

    alert('Latitude: ' + position.coords.latitude + '\n' + 
      'Longitude: ' + position.coords.longitude + '\n'); 

    var myLat = position.coords.latitude; 
    var myLong = position.coords.longitude; 

    map.setCenter(new LatLng(myLat, myLong)); 
}; 

当您使用的

map.setCenter(new LatLng(myLat, myLong)) 

,你必须使用

map.setCenter(new google.maps.LatLng(myLat, myLong)) 

LatLn g对象将是未定义的。您需要使用的对象是google.maps.LatLng。

+0

啊伟大的工作有效(有点......我的应用程序崩溃,但我认为这将是一个不同的问题)谢谢! – MeltingDog 2012-08-01 05:42:49

+0

祝你好运:) – 2012-08-01 05:47:57

3

这只是

var map; 
function initialize() { 
    //... 
} 

initialize(); 

function initiate_geolocation() { 
    navigator.geolocation.getCurrentPosition(onSuccess); 
} 

function onSuccess(position){ 
    var myLat = position.coords.latitude; 
    var myLong = position.coords.longitude; 
    map.setCenter(new google.maps.LatLng(myLat, myLong)); // edit this line 
} 
initiate_geolocation(); 

这不是map.setCenter(new LatLng(myLat, myLong)); instea d应该是map.setCenter(new google.maps.LatLng(myLat, myLong));

DEMO.

+0

谢谢 - 我试过了,它似乎不适合我。还尝试将myLat和myLong放在那里 – MeltingDog 2012-08-01 05:17:51

+0

myLat,myLong包含什么? – 2012-08-01 05:18:46

+0

完整的数字纬度和经度号码 – MeltingDog 2012-08-01 05:22:56

0

你可以简单地传递myLat和myLong在google.maps.LatLng构造函数的参数。所以,你的代码应该是这样的:

function initialize() { 
var mapOptions = { 
center: new google.maps.LatLng(myLat,myLong), 
zoom: 4, 
mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions); 
} 
+0

谢谢,我也试过。我添加了上面的完整代码,如果这使得它更容易 – MeltingDog 2012-08-01 05:25:27