forked from jokeruzzo/ofxMapBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRMMapKit.mm
executable file
·533 lines (312 loc) · 14.8 KB
/
RMMapKit.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// *
// \
// \
// \
// \
// \
// *
// made by Martijn Mellema
// Interaction Designer from Arnhem, The Netherlands
// www.martijnmellema.nl
///http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/Performance/Performance.html
#include "RMMapKit.h"
#import "proj_api.h"
#pragma mark - Setup
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
static mapController * mapViewController = nil ;
static RMMapView * mapView = nil ;
RMMapKit::RMMapKit() {
mapView = NULL;
offline = false;
}
RMMapKit::~RMMapKit() {
}
RMMapView *RMMapKit::getMKMapView(){
return mapView;
}
void RMMapKit::detectRetina(){
CGRect screenSize = [[UIScreen mainScreen] bounds];
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES) {
// RETINA DISPLAY
cout<< " [UIScreen mainScreen] bounds] IS RETINA " <<endl;
screenSize.size.width = screenSize.size.width * [[UIScreen mainScreen] scale];
screenSize.size.height = screenSize.size.height * [[UIScreen mainScreen] scale];
}
}
void RMMapKit::offlineMap(string map){
const char * c = map.c_str();
NSString *NSMap = [NSString stringWithUTF8String:c];
offlineSource = [[RMMBTilesSource alloc] initWithTileSetURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:NSMap ofType:@"mbtiles"]]];
offline = true;
mapViewController = [[mapController alloc] initWithFrame: CGRectMake(0, 0,
ofGetWidth(),
ofGetHeight())
andTilesource:offlineSource ];
mapView = mapViewController.getMapView;
[mapView setShowLogoBug:false];
[ofxiPhoneGetUIWindow() addSubview:mapView];
}
void RMMapKit::onlineMap(string urlVal){
offline = true;
const char * c = urlVal.c_str();
NSString *NSMap = [NSString stringWithUTF8String:c];
NSURL *url = [NSURL URLWithString:NSMap];
NSLog(@"loading URL: %@",url);
RMMapBoxSource *onlineSource = [[RMMapBoxSource alloc] initWithReferenceURL:url ];
mapViewController = [[mapController alloc] initWithFrame: CGRectMake(0, 0,
ofGetWidth(),
ofGetHeight())
andTilesource:onlineSource ];
mapView = mapViewController.getMapView;
[mapView setDelegate :mapViewController];
[ofxiPhoneGetUIWindow() addSubview:mapView];
CGRect rect = ofxiPhoneGetGLView().frame;
cout<< rect.size.width<<endl;
cout<< rect.size.height<<endl;
rect.origin.x = 0;
rect.origin.y = 0;
rect.size.width = ofGetWidth();
rect.size.height = ofGetHeight();
cout<<ofGetWidth()<<endl;
cout<< ofGetHeight()<<endl;
ofxiPhoneGetGLView().frame = rect;
}
void RMMapKit::close() {
ofLog(OF_LOG_VERBOSE, "RMMapKit::close()");
[[mapView superview] removeFromSuperview];
}
#pragma mark - Zoom
bool RMMapKit::bZoom(){
return [mapViewController allowZoom];
}
void RMMapKit::allowZoom(bool b){
[mapViewController setZoom:b];
}
float RMMapKit::getZoom(){
return [mapViewController getZoom];
}
void RMMapKit::setMapZoom(float zoomLevel, bool animated = true) {
[mapView setZoom:zoomLevel animated:animated];
}
void RMMapKit::setMinZoom (float minZoom){
mapView.minZoom = minZoom;
}
void RMMapKit::setMaxZoom (float maxZoom){
mapView.maxZoom = maxZoom;
}
#pragma mark - Scroll
void RMMapKit::setAllowScroll(bool b) {
mapView._mapScrollView.scrollEnabled = b;
}
#pragma mark - Location
ofxLocation RMMapKit::getCenterLocation() {
return ofxLocation::ofxLocationMake(mapView.centerCoordinate);
}
// convert location (latitude, longitude) to screen coordinates (i.e. pixels)
ofPoint RMMapKit::getScreenCoordinatesForLocation(double latitude, double longitude) {
CGPoint cgPoint = [mapView coordinateToPixel: makeCLLocation(latitude, longitude)];
if(IS_RETINA)
{
return ofPoint(ofGetWidth()/4+(cgPoint.x*2),ofGetHeight()/4+ (cgPoint.y*2), 0);
} else{
// return ofPoint(cgPoint.x,cgPoint.y, 0);
return ofPoint(cgPoint.x+128,cgPoint.y-128, 0);
}
}
// convert location (latitude, longitude) to screen coordinates (i.e. pixels)
ofPoint RMMapKit::getScreenCoordinatesForLocation(CLLocationCoordinate2D loc) {
CGPoint cgPoint = [mapView coordinateToPixel: loc];
// if(bIsRetina){
return ofPoint(ofGetWidth()/4+(cgPoint.x*2),ofGetHeight()/4+ (cgPoint.y*2));
// }
}
RMProjectedPoint RMMapKit::locationToProjectedPoint(double latitude, double longitude){
return [[mapView projection] coordinateToProjectedPoint:CLLocationCoordinate2DMake(latitude,longitude)];
}
ofPoint RMMapKit::getScreenCoordinatesForProjectedPoint(RMProjectedPoint point) {
CGPoint cgPoint = [mapView projectedPointToPixel:point];
if(IS_RETINA)
{
return ofPoint(cgPoint.x+128,cgPoint.y-128, 0);
//return ofPoint(ofGetWidth()/4 +(cgPoint.x*2),ofGetHeight()/4+ (cgPoint.y*2), 0);
} else{
return ofPoint(cgPoint.x+128,cgPoint.y-128, 0);
}
}
double RMMapKit::distance(ofxLocation locBegin, ofxLocation locEnd) {
double theta, dist;
theta = locBegin.longitude - locEnd.longitude;
dist = sin(ofDegToRad(locBegin.latitude)) * sin(ofDegToRad(locEnd.latitude)) + cos(ofDegToRad(locBegin.latitude)) * cos(ofDegToRad(locEnd.latitude)) * cos(ofDegToRad(theta));
dist = acos(dist);
dist = ofRadToDeg(dist);
dist = dist * 60 * 1.1515;
return dist ;
}
ofxLocation RMMapKit::getLocationForScreenCoordinates(float x, float y) {
return ofxLocation::ofxLocationMake([mapView pixelToCoordinate:CGPointMake(x, y) ]);
}
ofxLocation RMMapKit::projectedPointToCoordinate(ofPoint projectedPoint){
RMProjectedPoint aPoint;
aPoint.x = projectedPoint.x;
aPoint.y = projectedPoint.y;
return ofxLocation::ofxLocationMake([[mapView projection] projectedPointToCoordinate:aPoint]);
}
#define MAX_LATITUDE 89.999
#define MAX_LONGITUDE 179.999
CLLocationCoordinate2D RMMapKit::makeCLLocation(double latitude, double longitude) {
CLLocationCoordinate2D center = {
CLAMP(latitude, -MAX_LATITUDE, MAX_LATITUDE),
CLAMP(longitude, -MAX_LONGITUDE, MAX_LONGITUDE)
};
return center;
}
void RMMapKit::setCenter(double latitude, double longitude, bool animated ) {
[mapView setCenterCoordinate:CLLocationCoordinate2DMake(latitude, longitude) animated:animated];
}
float RMMapKit::metersPerPixel(){
return [mapView metersPerPixel];
}
void RMMapKit::retinaDisplay(bool b){
mapView.adjustTilesForRetinaDisplay = b;
}
bool RMMapKit:: bisScrollEnabled(){
return mapView._mapScrollView.scrollEnabled;
}
void RMMapKit::setScrollEnabled(bool b){
mapView._mapScrollView.scrollEnabled = b;
}
//-----------------------------------------------
ofxLocation RMMapKit::customProjectionPoint(string EPSG, ofPoint location){
//the coordinates, in wgs84 (4236) mercator
double xCoord[1] ;
double yCoord[1];
string google = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs";
string tilestream = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.4 +lon_0=0.0 +x_0=-33.0 +y_0=250.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs";
//the proj4 representations of the objects
projPJ source = pj_init_plus((char*)EPSG.c_str());
projPJ target = pj_init_plus((char*)tilestream.c_str());
xCoord[0]= (double)location.x;
yCoord[0] = (double)location.y;
pj_transform(source,target, 1, 1, xCoord, yCoord, NULL);
RMProjectedPoint p;
p.x = xCoord[0];
p.y = yCoord[0];
CLLocationCoordinate2D coord = [mapView projectedPointToCoordinate:p];
ofxLocation returnLocation;
returnLocation.longitude = coord.longitude;
returnLocation.latitude = coord.latitude;
pj_free(source);
pj_free(target);
return returnLocation;
}
#pragma mark - Rotation around center point
//-----------------------------------------------
void RMMapKit::rotateUser(UserTrackingMode mode ){
mapView.userTrackingMode = RMUserTrackingMode(mode);
}
//-----------------------------------------------
void RMMapKit::rotateMap(double degrees, float seconds)
{
[mapView rotateUser:degrees rotationTime:seconds];
}
//-----------------------------------------------
void RMMapKit::rotateMap(double degrees){
[mapView rotateUser:degrees];
mapViewController.viewOffset = CGPointMake(384, 512);
}
BOOL RMMapKit::isRotating(){
return [mapView isRotatingUser];
}
void RMMapKit::setUserPosition( double degrees, float zoom){
[mapView rotateUser:degrees zoom:zoom];
}
void RMMapKit::setUserPosition( double degrees, ofxLocation loc){
[mapView rotateUser:degrees :CLLocationCoordinate2DMake(loc.latitude, loc.longitude)];
}
//-----------------------------------------------
// TODO fix this
void RMMapKit::addMarker(ofxLocation coordinate, string routeId){
NSMutableArray * locations = [NSMutableArray new] ;
[locations addObject:[[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]] ;
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView
coordinate:((CLLocation *)[locations objectAtIndex:0]).coordinate
andTitle:ofxStringToNSString(routeId)];
annotation.userInfo = locations;
[annotation setBoundingBoxFromLocations:locations];
[mapView addAnnotation:annotation];
annotation = nil;
}
// TODO: fix this
void RMMapKit::addImageMarker(ofxLocation coordinate, string routeId, UIImage* image){
cout<<"RMMAPKIT::"<<"Doesn't work yet"<<endl;
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView
coordinate:CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude)
andTitle:ofxStringToNSString(routeId)];
annotation.annotationIcon = image;
annotation.badgeIcon = image;
[mapView addAnnotation:annotation];
annotation = nil;
}
void RMMapKit::addAnnotation(vector<ofxLocation> coordinates, string routeId){
NSMutableArray * locations = [NSMutableArray new] ;
for(int i = 0; i<coordinates.size();i++){
[locations addObject:[[CLLocation alloc] initWithLatitude:coordinates[i].latitude longitude:coordinates[i].longitude]] ;
}
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView
coordinate:((CLLocation *)[locations objectAtIndex:0]).coordinate
andTitle:ofxStringToNSString(routeId)];
annotation.userInfo = locations;
[annotation setBoundingBoxFromLocations:locations];
[mapView addAnnotation:annotation];
annotation = nil;
}
void RMMapKit::updateAnnotation(ofxLocation coordinates, string name){
NSMutableArray * locations = [NSMutableArray new] ;
[locations addObject:[[CLLocation alloc] initWithLatitude:coordinates.latitude longitude:coordinates.longitude]] ;
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView
coordinate:((CLLocation *)[locations objectAtIndex:0]).coordinate
andTitle:ofxStringToNSString(name)];
annotation.userInfo = locations;
[annotation setBoundingBoxFromLocations:locations];
[mapView addAnnotation:annotation];
annotation = nil;
}
//-----------------------------------------------
// todo test this
void RMMapKit::setAnnotation(string routeId, bool b){
for (RMAnnotation *annotation in mapView.annotations)
if ( ! annotation.isUserLocationAnnotation)
{
if ([annotation.title isEqualToString: ofxStringToNSString(routeId)])
annotation.layer.hidden = b;
}
}
void RMMapKit::addGeoJSON(string data){
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:[ofxStringToNSString(data) dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil];
vector<NSString*> tempUserNames;
for (NSUInteger x = 0; x < [[json objectForKey:@"features"] count] ; x++){
NSString *name = [[[[json objectForKey:@"features"] objectAtIndex: x ] valueForKeyPath:@"properties.name"] mutableCopy];
NSMutableArray * points = [[[[[[json objectForKey:@"features"] objectAtIndex: x ] objectForKey:@"geometry"] objectForKey:@"coordinates"] objectAtIndex:0] mutableCopy];
tempUserNames.push_back(name);
for (NSUInteger i = 0; i < [points count]; i++){
[points replaceObjectAtIndex:i
withObject:[[CLLocation alloc] initWithLatitude:[[[points objectAtIndex:i] objectAtIndex:1] doubleValue]
longitude:[[[points objectAtIndex:i] objectAtIndex:0] doubleValue]]];
}
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView
coordinate:((CLLocation *)[points objectAtIndex:0]).coordinate
andTitle:[NSString stringWithFormat:@"%@%@", @"geoJSON:" , name]];
annotation.userInfo = points;
[mapView addAnnotation:annotation];
}
}
bool RMMapKit::isUserLocationUpdating(){
for (RMAnnotation *annotation in mapView.annotations)
if ( annotation.isUserLocationAnnotation)
{
return true;
}
return true;
}