This code is designed to run inside a Yahoo Open App, but it can be adapted for any OpenSocial widget written with the JavaScript API. It uses OpenSocial to get the users unstructured location and YQL to convert it into GPS coordinates.
function getProfile() { var req = opensocial.newDataRequest(); var opt_params = {}; var req = opensocial.newDataRequest(); opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.CURRENT_LOCATION]; req.add(req.newFetchPersonRequest('VIEWER', opt_params), 'myOwner'); req.send(onLoadProfile); } function onLoadProfile(response) { var html = processProfile(response); document.getElementById('divProfile').innerHTML = html; } function processProfile(response) { var html = new Array(); try { var person = response.get('myOwner').getData(); var sId = person.getId(); var sGivenName = person.getField(opensocial.Person.Field.NAME).getField(opensocial.Name.Field.GIVEN_NAME); var sFamilyName = person.getField(opensocial.Person.Field.NAME).getField(opensocial.Name.Field.FAMILY_NAME); var sUnstructuredName = person.getField(opensocial.Person.Field.NAME).getField(opensocial.Name.Field.UNSTRUCTURED); var gender = person.getField(opensocial.Person.Field.GENDER); var sGender = gender ? gender.getDisplayValue() : 'unknown'; var sAddressUnstructured = person.getField(opensocial.Person.Field.CURRENT_LOCATION).getField('formatted'); // Get GPS coordinates: var sLocation = getLocationFromAddress(sAddressUnstructured); html.push('givenName: ' + sGivenName); html.push('<br>'); html.push('familyName: ' + sFamilyName); html.push('<br>'); html.push('gender:' + sGender); html.push('<br>'); html.push('unstructured address: ' + sAddressUnstructured); html.push('<br>'); } catch (err) { html.push('<div class="yap-error">error! ' + err + '</div>'); } return html.join(''); } function getLocationFromAddress(location) { var BASE_URI = 'http://query.yahooapis.com/v1/public/yql?format=json&q='; function handler(rsp) { var latitude = rsp.data.query.results.place.centroid.latitude; var longitude = rsp.data.query.results.place.centroid.longitude; document.getElementById('divProfile').innerHTML = document.getElementById('divProfile').innerHTML + "<br /> latitude: " + latitude + "<br /> longitude" + longitude; } var params = {}; params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON; params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET; var yqlQuery = BASE_URI + escape("select centroid from geo.places where text='" + location + "'"); gadgets.io.makeRequest(yqlQuery, handler, params); } gadgets.util.registerOnLoadHandler(function() { getProfile(); }); <div> <h1> YAP OpenSocial/YQL/Geo Demo</h1> <h3> Your Profile</h3> <div> <div> Loading Profile...</div> </div> </div> |