2011-09-25 61 views
0

我是新來的Javascript,我試圖最終做出讓用戶在使用getLocation API和HTML5的位置的東西,並使用simpleGeo來獲得座標的構建。SimpleGeo Javascript SDK的幫助

到目前爲止,我試圖讓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控制檯意想不到的標識符。任何幫助,將不勝感激。 :)

回答

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>