0

我是 Javascript 的新手,我正在尝试最终制作一些东西,让用户在使用带有 HTML5 的 getLocation API 时获得位置,并使用 simpleGeo 来构建坐标。

到目前为止,我正试图让 SimpleGeo 工作,我有这个:

        var client = new simplegeo.ContextClient('YUpXKUcaTr2ZE5T5vkhaDRAXWbDbaZts');

    function displayData(err, data) {

        if (err) { 
            console.error(err);
        } else {
            console.log(JSON.stringify(data));
        }

    }

client.getLocation({enableHighAccuracy: true}, function(err, position) {
if (err) {
    // Could not retrieve location information. Check err for more information
} else {
    // Latitude and longitude available in position.coords


        var lat = position.coords.latitude;
        var lon = position.coords.longitude;
        $('#header').html('<p>your latitude is: ' + lat + '. And your longitude is: ' + lon + '.</p>');



}
});

client.getNearbyAddress(37.765850, -122.437094), function(err, position) {
if (err) {
$('#header').html('<p>Sorry we couldn't locate an address.</p>)
} else {

$('#header').html('<p>Your Street number is ' + street_number + '</p>');


}
});

然而,这在 Chrome 的 JS 控制台中显示了意外的标识符。任何帮助,将不胜感激。:)

4

1 回答 1

0

我实际上是开发者倡导者@SimpleGeo。您的函数 displayData 看起来没有做任何事情。var street_number 也没有定义。您是否要获取用户的地址?

这是一个返回用户邻域的示例:

<script type="text/javascript"> 
var client = new simplegeo.ContextClient('YOUR_JSONP_TOKEN');

$(document).ready(function() {

   client.getLocation({enableHighAccuracy: true}, function(err, position) {

       // get the user's context for the found location
       client.getContext(position.coords.latitude, position.coords.longitude,
       function(err, data) {

           if (err)
           (typeof console == "undefined") ? alert(err) : console.error(err);

           else {

               for (var i = 0, ii = data.features.length; i < ii ; i++) {

                   // switch on the category
                   switch(data.features[i]["classifiers"][0]["category"]) {

                       // Return the Neighborhood as an example
                       case "Neighborhood":
                $("#neighborhood").val(data.features[i]["name"]);
                       break;
                   }
               }
           }
       });
   });
});
</script>
于 2011-09-26T18:05:16.183 回答