帮助SimpleGeo Javascript SDK

时间:2011-09-25 19:10:44

标签: javascript simplegeo

我是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控制台中的意外标识符。任何帮助,将不胜感激。 :)

1 个答案:

答案 0 :(得分:0)

我实际上是Developer Advocate @ 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>