Skip to content
Browse files

First version, primitive CoreLocation wrapper

  • Loading branch information...
0 parents commit a6f23f1b4650fe4d5219603897401b2d97abcfac @evanphx committed Aug 7, 2012
Showing with 246 additions and 0 deletions.
  1. +23 −0 .autotest
  2. +6 −0 History.txt
  3. +8 −0 Manifest.txt
  4. +57 −0 README.txt
  5. +12 −0 Rakefile
  6. +5 −0 bin/lost
  7. +95 −0 ext/lost/core_loc.m
  8. +5 −0 ext/lost/extconf.rb
  9. +30 −0 ext/lost/lost.c
  10. +5 −0 lib/lost.rb
  11. BIN lib/lost/lost.bundle
23 .autotest
@@ -0,0 +1,23 @@
+# -*- ruby -*-
+
+require 'autotest/restart'
+
+# Autotest.add_hook :initialize do |at|
+# at.extra_files << "../some/external/dependency.rb"
+#
+# at.libs << ":../some/external"
+#
+# at.add_exception 'vendor'
+#
+# at.add_mapping(/dependency.rb/) do |f, _|
+# at.files_matching(/test_.*rb$/)
+# end
+#
+# %w(TestA TestB).each do |klass|
+# at.extra_class_map[klass] = "test/test_misc.rb"
+# end
+# end
+
+# Autotest.add_hook :run_command do |at|
+# system "rake build"
+# end
6 History.txt
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2012-08-05
+
+* 1 major enhancement
+
+ * Birthday!
+
8 Manifest.txt
@@ -0,0 +1,8 @@
+.autotest
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+bin/current/location
+lib/current/location.rb
+test/test_current/location.rb
57 README.txt
@@ -0,0 +1,57 @@
+= lost
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== DEVELOPERS:
+
+After checking out the source, run:
+
+ $ rake newb
+
+This task will install any missing dependencies, run the tests/specs,
+and generate the RDoc.
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2012 FIX
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 Rakefile
@@ -0,0 +1,12 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+Hoe.plugin :compiler
+
+Hoe.spec 'lost' do
+ developer('Evan Phoenix', 'evan@phx.io')
+end
+
+# vim: syntax=ruby
5 bin/lost
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+require 'lost'
+
+p Lost.current_position
95 ext/lost/core_loc.m
@@ -0,0 +1,95 @@
+//
+// Using CoreLocation on Mac OS X with command-line
+// $ clang CoreLocationTest.m -framework cocoa -framework CoreLocation
+// $ ./a.out
+// location service enabled
+// 2011-12-01 21:03:01.839 a.out[10214:903] latitude,logitude : 35.606647, 140.695538
+// 2011-12-01 21:03:01.842 a.out[10214:903] timestamp : 2011-12-01 21:01:36 +0900
+// tmiz moo@tmiz.net
+//
+//
+#import <cocoa/cocoa.h>
+#import <CoreLocation/CoreLocation.h>
+
+@interface LLHolder : NSObject {
+ double latitude;
+ double longitude;
+}
+
+- (void)latitude:(double*)lat longitude:(double*)log;
+
+- (void)logLonLat:(CLLocation*)location;
+- (void)locationManager:(CLLocationManager *)manager
+ didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
+- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;
+@end
+
+@implementation LLHolder
+- (void)latitude:(double*)lat longitude:(double*)log
+{
+ *lat = latitude;
+ *log = longitude;
+}
+
+- (void)logLonLat:(CLLocation*)location
+{
+ CLLocationCoordinate2D coordinate = location.coordinate;
+ latitude = coordinate.latitude;
+ longitude = coordinate.longitude;
+
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+
+- (void)locationManager:(CLLocationManager *)manager
+ didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ [self logLonLat:newLocation];
+ [pool drain];
+}
+
+- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
+ latitude = 0.0;
+ longitude = 0.0;
+ CFRunLoopStop(CFRunLoopGetCurrent());
+}
+@end
+
+id g_lm = nil;
+
+int int_coreloc_enable() {
+ if ([CLLocationManager locationServicesEnabled]) {
+ g_lm = [[CLLocationManager alloc] init];
+ return 1;
+ }
+
+ return 0;
+}
+
+void int_coreloc_get(double* lat, double* log) {
+ LLHolder* obj = [[LLHolder alloc] init];
+ [g_lm setDelegate:obj];
+ [g_lm startUpdatingLocation];
+
+ CFRunLoopRun();
+
+ [obj release];
+
+ [obj latitude: lat longitude: log];
+}
+
+// int main(int ac,char *av[])
+// {
+ // id obj = [[NSObject alloc] init];
+ // id lm = nil;
+ // if ([CLLocationManager locationServicesEnabled]) {
+ // printf("location service enabled\n");
+ // lm = [[CLLocationManager alloc] init];
+ // [lm setDelegate:obj];
+ // [lm startUpdatingLocation];
+ // }
+
+ // CFRunLoopRun();
+ // [lm release];
+ // [obj release];
+ // return 0;
+// }
5 ext/lost/extconf.rb
@@ -0,0 +1,5 @@
+require 'mkmf'
+
+$LDFLAGS += " -framework cocoa -framework CoreLocation "
+
+create_makefile("lost")
30 ext/lost/lost.c
@@ -0,0 +1,30 @@
+#include "ruby.h"
+#include "intern.h"
+
+int int_coreloc_enable();
+void int_coreloc_get(double* lat, double* log);
+
+VALUE coreloc_enable(VALUE r_self) {
+ if(int_coreloc_enable()) {
+ return Qtrue;
+ }
+
+ return Qfalse;
+}
+
+VALUE coreloc_pos(VALUE r_self) {
+ double lat, log;
+
+ int_coreloc_get(&lat, &log);
+
+ return rb_ary_new3(2, rb_float_new(lat),
+ rb_float_new(log));
+}
+
+void Init_lost() {
+ VALUE mod = rb_define_module("Lost");
+
+ rb_define_singleton_method(mod, "coreloc_enable", coreloc_enable, 0);
+ rb_define_singleton_method(mod, "current_position", coreloc_pos, 0);
+}
+
5 lib/lost.rb
@@ -0,0 +1,5 @@
+require 'lost/lost.so'
+
+module Lost
+ coreloc_enable
+end
BIN lib/lost/lost.bundle
Binary file not shown.

0 comments on commit a6f23f1

Please sign in to comment.
Something went wrong with that request. Please try again.