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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
#import "SearchVC.h"
#import "CompassView.h"
#import "LocationManager.h"
#import "SearchCell.h"
#import "BookmarksVC.h"
#import "CustomNavigationView.h"
#import "MapsAppDelegate.h"
#import "MapViewController.h"
#include "Framework.h"
#include "../../search/result.hpp"
#include "../../indexer/mercator.hpp"
#include "../../platform/platform.hpp"
#include "../../platform/preferred_languages.hpp"
#include "../../geometry/angles.hpp"
#include "../../geometry/distance_on_sphere.hpp"
#define MAPSWITHME_PREMIUM_APPSTORE_URL @"itms://itunes.com/apps/mapswithmepro"
/// When to display compass instead of country flags
#define MIN_COMPASS_DISTANCE 25000.0
SearchVC * g_searchVC = nil;
@interface ResultsWrapper : NSObject
{
vector<search::Result> m_results;
NSString * m_searchString;
}
// Stores search string which corresponds to these results.
@property(nonatomic, retain) NSString * m_searchString;
- (id)initWithResults:(search::Results const &)res;
- (vector<search::Result> const &)get;
@end
@implementation ResultsWrapper
@synthesize m_searchString;
- (void)dealloc
{
[m_searchString release];
[super dealloc];
}
- (id)initWithResults:(search::Results const &)res
{
if ((self = [super init]))
m_results.assign(res.Begin(), res.End());
return self;
}
- (vector<search::Result> const &)get
{
return m_results;
}
@end
// Last search results are stored betweel SearchVC sessions
// to appear instantly for the user, they also store last search text query
ResultsWrapper * g_lastSearchResults = nil;
static void OnSearchResultCallback(search::Results const & res)
{
if (g_searchVC)
{
ResultsWrapper * w = [[ResultsWrapper alloc] initWithResults:res];
[g_searchVC performSelectorOnMainThread:@selector(addResult:)
withObject:w waitUntilDone:NO];
[w release];
}
}
/////////////////////////////////////////////////////////////////////
@implementation SearchVC
- (id) init
{
if ((self = [super initWithNibName:nil bundle:nil]))
{
m_framework = &GetFramework();
m_locationManager = [MapsAppDelegate theApp].m_locationManager;
double lat, lon;
bool const hasPt = [m_locationManager getLat:lat Lon:lon];
m_framework->PrepareSearch(hasPt, lat, lon);
}
return self;
}
- (void)hideIndicator
{
[m_indicator stopAnimating];
m_searchTextField.leftView = m_originalIndicatorView;
}
- (void)showIndicator
{
m_searchTextField.leftView = m_indicator;
[m_indicator startAnimating];
}
- (void)fillSearchParams:(search::SearchParams &)params withText:(NSString *)queryString
{
params.m_query = [[queryString precomposedStringWithCompatibilityMapping] UTF8String];
params.m_callback = bind(&OnSearchResultCallback, _1);
// Set current keyboard input mode
string lang;
// UITextInputMode appeared only in iOS >= 4.2
if (NSClassFromString(@"UITextInputMode") != nil
&& [UITextInputMode respondsToSelector:@selector(currentInputMode)])
lang = [[UITextInputMode currentInputMode].primaryLanguage UTF8String];
else
lang = languages::CurrentLanguage(); // Use current UI language instead
params.SetInputLanguage(lang);
double lat, lon;
if ([m_locationManager getLat:lat Lon:lon])
params.SetPosition(lat, lon);
}
- (void)loadView
{
// create user interface
// Custom view is used to automatically layout all elements
UIView * parentView = [[[CustomNavigationView alloc] init] autorelease];
parentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
UINavigationBar * navBar = [[[UINavigationBar alloc] init] autorelease];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UINavigationItem * item = [[[UINavigationItem alloc] init] autorelease];
UIBarButtonItem * closeButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"maps", @"Search Results - Close search button") style: UIBarButtonItemStyleDone
target:self action:@selector(onCloseButton:)] autorelease];
item.leftBarButtonItem = closeButton;
m_searchBar = [[UISearchBar alloc] init];
m_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// restore previous search query
if (g_lastSearchResults)
m_searchBar.text = g_lastSearchResults.m_searchString;
m_searchBar.delegate = self;
m_searchBar.placeholder = NSLocalizedString(@"search_map", @"Search box placeholder text");
item.titleView = m_searchBar;
// Add search in progress indicator
for(UIView * v in m_searchBar.subviews)
{
if ([v isKindOfClass:[UITextField class]])
{
// Save textField to show/hide activity indicator in it
m_searchTextField = (UITextField *)v;
m_originalIndicatorView = [m_searchTextField.leftView retain];
m_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
m_indicator.bounds = m_originalIndicatorView.bounds;
break;
}
}
[navBar pushNavigationItem:item animated:NO];
[parentView addSubview:navBar];
m_table = [[UITableView alloc] init];
m_table.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
m_table.delegate = self;
m_table.dataSource = self;
[parentView addSubview:m_table];
self.view = parentView;
}
- (void)dealloc
{
g_searchVC = nil;
[m_searchBar release];
[m_table release];
[super dealloc];
}
- (void)viewDidLoad
{
g_searchVC = self;
}
- (void)viewDidUnload
{
g_searchVC = nil;
// to correctly free memory
[m_indicator release]; m_indicator = nil;
[m_originalIndicatorView release]; m_originalIndicatorView = nil;
[m_searchBar release]; m_searchBar = nil;
[m_table release]; m_table = nil;
[super viewDidUnload];
}
// Banner dialog handler
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
// Launch appstore
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:MAPSWITHME_PREMIUM_APPSTORE_URL]];
}
// Close Search view
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)IsProVersion
{
return GetPlatform().IsPro();
}
- (void)viewWillAppear:(BOOL)animated
{
// Disable search for free version
if (![self IsProVersion])
{
// Display banner for paid version
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"search_available_in_pro_version", @"Search button pressed dialog title in the free version")
message:nil
delegate:self
cancelButtonTitle:NSLocalizedString(@"cancel", @"Search button pressed dialog Negative button in the free version")
otherButtonTitles:NSLocalizedString(@"get_it_now", @"Search button pressed dialog Positive button in the free version"), nil];
[alert show];
[alert release];
}
else
{
[m_locationManager start:self];
// show keyboard
[m_searchBar becomeFirstResponder];
}
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[m_locationManager stop:self];
// hide keyboard immediately
[m_searchBar resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation) fromInterfaceOrientation
{
[m_locationManager setOrientation:self.interfaceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES; // All orientations are supported.
}
//**************************************************************************
//*********** SearchBar handlers *******************************************
- (void)searchBar:(UISearchBar *)sender textDidChange:(NSString *)searchText
{
// Search even with empty string.
search::SearchParams params;
[self fillSearchParams:params withText:searchText];
[self showIndicator];
m_framework->Search(params);
}
- (void)onCloseButton:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
//*********** End of SearchBar handlers *************************************
//***************************************************************************
- (void)setSearchBoxText:(NSString *)text
{
m_searchBar.text = text;
// Manually send text change notification if control has no focus
if (![m_searchBar isFirstResponder])
[self searchBar:m_searchBar textDidChange:text];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
m_suggestionsCount = [m_searchBar.text length] ? 0 : 1;
if (g_lastSearchResults)
return [g_lastSearchResults get].size() + m_suggestionsCount;
else
return 0 + m_suggestionsCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger realRowIndex = indexPath.row;
if (m_suggestionsCount)
{
// Return cell with suggestion icons
if (indexPath.row == 0)
{
SearchSuggestionsCell * cell = (SearchSuggestionsCell *)[tableView dequeueReusableCellWithIdentifier:@"SuggestionsCell"];
if (!cell)
{
cell = [[[SearchSuggestionsCell alloc] initWithReuseIdentifier:@"SuggestionsCell"] autorelease];
cell.delegate = self;
[cell addIcon:[UIImage imageNamed:@"food.png"] withSuggestion:NSLocalizedString(@"food", @"Search Suggestion")];
[cell addIcon:[UIImage imageNamed:@"money.png"] withSuggestion:NSLocalizedString(@"money", @"Search Suggestion")];
[cell addIcon:[UIImage imageNamed:@"fuel.png"] withSuggestion:NSLocalizedString(@"fuel", @"Search Suggestion")];
[cell addIcon:[UIImage imageNamed:@"shop.png"] withSuggestion:NSLocalizedString(@"shop", @"Search Suggestion")];
[cell addIcon:[UIImage imageNamed:@"transport.png"] withSuggestion:NSLocalizedString(@"transport", @"Search Suggestion")];
[cell addIcon:[UIImage imageNamed:@"tourism.png"] withSuggestion:NSLocalizedString(@"tourism", @"Search Suggestion")];
}
return cell;
}
// We're displaying one additional row with suggestions, fix results array indexing
realRowIndex -= m_suggestionsCount;
}
if (g_lastSearchResults == nil || realRowIndex >= (NSInteger)[g_lastSearchResults get].size())
{
ASSERT(false, ("Invalid m_results with size", [g_lastSearchResults get].size()));
return nil;
}
search::Result const & r = [g_lastSearchResults get][realRowIndex];
switch (r.GetResultType())
{
case search::Result::RESULT_FEATURE:
{
SearchCell * cell = (SearchCell *)[tableView dequeueReusableCellWithIdentifier:@"FeatureCell"];
if (!cell)
cell = [[[SearchCell alloc] initWithReuseIdentifier:@"FeatureCell"] autorelease];
// Init common parameters
cell.featureName.text = [NSString stringWithUTF8String:r.GetString()];
cell.featureCountry.text = [NSString stringWithUTF8String:r.GetRegionString()];
cell.featureType.text = [NSString stringWithUTF8String:r.GetFeatureType()];
// Get current position and compass "north" direction
double azimut = -1.0;
string distance;
double lat, lon;
if ([m_locationManager getLat:lat Lon:lon])
{
double north = -1.0;
[m_locationManager getNorthRad:north];
m_framework->GetDistanceAndAzimut(r, lat, lon, north, distance, azimut);
}
// Assign even empty string if no position found
cell.featureDistance.text = [NSString stringWithUTF8String:distance.c_str()];
// Show flags only if it has one and no azimut to feature
char const * flag = r.GetRegionFlag();
if (flag && azimut < 0.0)
{
UIImage * flagImage = [UIImage imageNamed:[NSString stringWithFormat:@"%s.png", flag]];
UIImageView * imgView = [[UIImageView alloc] initWithImage:flagImage];
cell.accessoryView = imgView;
[imgView release];
}
else
{
// Try to reuse existing compass view
CompassView * compass;
if ([cell.accessoryView isKindOfClass:[CompassView class]])
compass = (CompassView *)cell.accessoryView;
else
{
// Create compass view
float const h = (int)(m_table.rowHeight * 0.6);
compass = [[CompassView alloc] initWithFrame:CGRectMake(0, 0, h, h)];
cell.accessoryView = compass;
[compass release];
}
// Show arrow for valid azimut and if feature is not a continent (flag is exist)
compass.showArrow = (azimut >= 0.0 && flag) ? YES : NO;
}
return cell;
}
break;
case search::Result::RESULT_SUGGESTION:
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SuggestionCell"];
if (!cell)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SuggestionCell"] autorelease];
cell.textLabel.text = [NSString stringWithUTF8String:r.GetString()];
return cell;
}
break;
default:
ASSERT(false, ("Unsupported search result type"));
return nil;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger realRowIndex = indexPath.row;
// Suggestion cell was clicked
if (m_suggestionsCount)
{
if (realRowIndex == 0)
return;
realRowIndex -= m_suggestionsCount;
}
if (realRowIndex < (NSInteger)[g_lastSearchResults get].size())
{
search::Result const & res = [g_lastSearchResults get][realRowIndex];
switch(res.GetResultType())
{
// Zoom to the feature
case search::Result::RESULT_FEATURE:
{
m_framework->ShowSearchResult(res);
Framework::AddressInfo info;
info.m_name = res.GetString();
info.m_types.push_back(res.GetFeatureType());
[[MapsAppDelegate theApp].m_mapViewController showSearchResultAsBookmarkAtMercatorPoint:res.GetFeatureCenter() withInfo:info];
[self onCloseButton:nil];
}
break;
case search::Result::RESULT_SUGGESTION:
[self setSearchBoxText:[NSString stringWithUTF8String:res.GetSuggestionString()]];
// Remove blue selection from the row
[tableView deselectRowAtIndexPath: indexPath animated:YES];
break;
}
}
}
// Called on UI thread from search threads
- (void)addResult:(id)res
{
ResultsWrapper * w = (ResultsWrapper *)res;
[g_lastSearchResults release];
g_lastSearchResults = [w retain];
w.m_searchString = m_searchBar.text;
[self hideIndicator];
[m_table reloadData];
}
//******************************************************************
//*********** Location manager callbacks ***************************
- (void)onLocationError:(location::TLocationError)errorCode
{
// Handle location status changes if necessary
}
- (void)onLocationUpdate:(location::GpsInfo const &)info
{
// Refresh search results with newer location.
NSString * queryString = m_searchBar.text;
// Search even with empty string.
//if (queryString.length)
{
search::SearchParams params;
[self fillSearchParams:params withText:queryString];
[self showIndicator];
m_framework->Search(params);
}
}
- (void)onCompassUpdate:(location::CompassInfo const &)info
{
double lat, lon;
if (![m_locationManager getLat:lat Lon:lon])
return;
double const northRad = (info.m_trueHeading < 0) ? info.m_magneticHeading : info.m_trueHeading;
NSArray * cells = [m_table visibleCells];
for (NSUInteger i = 0; i < cells.count; ++i)
{
UITableViewCell * cell = (UITableViewCell *)[cells objectAtIndex:i];
NSInteger realRowIndex = [m_table indexPathForCell:cell].row;
if (m_suggestionsCount)
{
// Take into an account additional suggestions cell
if (realRowIndex == 0)
continue;
realRowIndex -= m_suggestionsCount;
}
search::Result const & res = [g_lastSearchResults get][realRowIndex];
if (res.GetResultType() == search::Result::RESULT_FEATURE)
{
// Show compass only for cells without flags
if ([cell.accessoryView isKindOfClass:[CompassView class]])
{
CompassView * compass = (CompassView *)cell.accessoryView;
m2::PointD const center = res.GetFeatureCenter();
compass.angle = ang::AngleTo(m2::PointD(MercatorBounds::LonToX(lon),
MercatorBounds::LatToY(lat)), center) + northRad;
}
}
}
}
//*********** End of Location manager callbacks ********************
//******************************************************************
// Dismiss virtual keyboard when touching tableview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[m_searchBar resignFirstResponder];
}
// Dismiss virtual keyboard when "Search" button is pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[m_searchBar resignFirstResponder];
}
// Callback from suggestion cell, called when icon is selected
- (void)onSuggestionSelected:(NSString *)suggestion
{
[self setSearchBoxText:[suggestion stringByAppendingString:@" "]];
// Clear old results immediately after click
[m_table reloadData];
}
@end
|