首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Pitney Bowes 定位服务构建一个简单的图片定位应用程序(2)

使用 Pitney Bowes 定位服务构建一个简单的图片定位应用程序(2)

步骤 3. 克隆 Node.js 寻像器应用程序代码从 IBM jStart DevOps 存储库克隆 Node 寻像器应用程序:
1
git clone https://hub.jazz.net/git/jstart/Picture.Finder.(Node)




所有应用程序逻辑都在 app.js 中,包括一些使得 Web 应用程序开发变得更容易的 Node 模块:
  • Express Web 开发框架
  • 针对 Express 的 Hogan 模板引擎
  • 用来调用 Pitney Bowes 的 REST 客户端
  • 一个 Instagram API 包装器库
  • 执行日期格式化的时间
  • 针对 JSON 操作的 pinch
“post to /location” 命令将会对 Pitney Bowes 地理编码服务执行一个 GET 调用。然后,返回的经纬度被传递给                Instagram(记得将代码中的 instagramID 和 instagramSecret 替换为那您自己的 Instagram                开发人员帐号和密钥),以便检索描述指定地址一公里范围以内的所有图片的 JSON。
请注意,在对地理编码服务的调用中,使用了从地理编码服务凭据中获得的 Pitney Bowes URL 和应用程序 ID。
1
var Client = rest.Client; client = new Client(); if (!address && !city && !state) res.render('index.html'); // direct way client.get(url + "?address=" + address + "&city=" + city + "&stateProvince=" + state + "&country=USA&fallbackToPostal=Y& fallbackToStreet=Y&fallbackToGeographic=Y& closeMatchesOnly=Y&appId =" + appId, function(data, response){ // data in callback contains the json returned from pitney bowes if (data.httpCode != "500") { lat = data.Output.Geometry.Pos.Y; lng = data.Output.Geometry.Pos.X; instagram.media.search({ lat: lat, lng: lng, complete: function(data){ // pinch converts all unix timestamps to a friendly date pinch(data, /created_time/, function(path, key, value) { return moment(value*1000).format("MMMM Do YYYY, h:mm a"); }); res.render('index.html', {data: data, address: address, city: city, state: state}); }, // end complete });// end search }// end httpCode check else { msg = "Can't find any pictures near this address"; res.render('index.html', {msg: msg, address: address, city: city, state: state}); } }); // end PB call




Hogan 模板语言和超净(ultra-clean)Mustache 模板语言将结果显示在 index.html 视图中。它们显示了从 Instagram 返回的                JSON 直接返回的结果(对创建时间进行了更改,利用服务器端的 pinch 库,将该时间从 UNIX 时间戳改为一个友好的日期格式):
1
<div> <ul> {{#data}} <li style="display: inline-block; padding: 25px"><a href="{{link}}"> <img src="{{#images}}{{#thumbnail}} {{url}} {{/thumbnail}}{{/images}}"></a> <br/> By: <em> {{#user}} {{username}} {{/user}}</em> <br/> {{created_time}} <br/> {{#comments}} {{count}} {{/comments}} comment(s) {{#likes}} {{count}} {{/likes}} likes. </li> {{/data}} </ul> </div>




步骤 4. 将应用程序推入 Bluemix
  • 转到应用程序的 app 目录。使用以下命令从命令行登录 Bluemix:                        
    1
    cf login -a api.ng.bluemix.net





                        提供您的用户名、密码和组织,并在系统提示您时按下空格键。
  • 一个 manifest.yml 文件包含在应用程序中,用于流线化该流程。它将设置应用程序名称、内存、实例数量和您在 步骤                        2 中创建的 Pitney Bowes 地理编码服务名称。 编辑 manifest.yml                        文件,以便更改那些对您的应用程序必须惟一的值。主机名必须是不同于 picturefinder 的名称,这样您获得的 URL 路由就不会与演示 URL                        (http://picturefinder.ng.bluemix.net)                        发生冲突。
    1
    --- applications: #Reference http://docs.cloudfoundry.com/doc ... -apps/manifest.html - name: PictureFinder #Application Name. Unique to the user's Space memory: 256M #The maximum memory to allocate to each application instance instances: 1 #The number of instances of the application to start host: my-unique-ehostname #Hostname for app routing. Unique to domain ng.bluemix.net. Change. path: . #Path to the application to be pushed command: node app.js #The command to use to start the application services: - Geocoding-demo #the already created geocode service




  • 从包含 app.js 和 manifest.yml 文件的 app 目录推入您的应用程序:                        
    1
    cf push




当应用程序运行时,转到 http://my-unique-hostname.ng.bluemix.net(其中的                    my-unique-hostname 是您在 manifest 文件中使用的主机名)。输入您的地址,并查看哪些 Instagram                用户正在您的附近发帖子。
结束语当在主要社交媒体平台上签入或使用位置共享特性时,全世界超过十二亿的人都在使用 Pitney Bowes                位置智能解决方案。通过精心定制用户对当前位置的体验,这些服务可帮助您,让您的应用程序更多地参与其中。
返回列表