SimpleGeo Javascript SDK 帮助

发布于 2024-12-06 19:47:05 字数 1194 浏览 0 评论 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 控制台中出现意外的标识符。任何帮助将不胜感激。 :)

I'm new to Javascript and I'm trying to eventually make something that will get the users location in using the getLocation API with HTML5, and use simpleGeo to get the building of the coordinates.

So far I'm trying to get SimpleGeo working and I have this:

        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>');


}
});

However this says unexpected identifier in the JS console in Chrome. Any help would be appreciated. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2024-12-13 19:47:06

我实际上是开发者倡导者@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>

I am actually the Developer Advocate @ SimpleGeo. Your function displayData doesn't look like it is doing anything. Also var street_number isn't defined. Are you looking to get the user's address?

Here is an example that returns the user's neighborhood:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文