showing the locations and finding the nearest spot in ios - ios

I want to display my spots NSArray *chSpot = [parser parseArray:responseObject];
which receives 3 objects , I want to show each location regarding to its lat and long
and find the nearest spot according to my location...I am using this code:
NSArray *chSpot = [parser parseArray:responseObject];
// GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locLat doubleValue]
// longitude:[locLong doubleValue]
// zoom:4];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:nil];
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton = YES;
for (int i=0; i<chSpot.count; i++)
{
ChargingSpots *spot = [chSpot objectAtIndex:i];
NSString *locLat = spot.LocationLat;
NSString *locLong = spot.LocationLong;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([locLat doubleValue], [locLong doubleValue]);
marker.title = #"Amman";
marker.snippet = #"Jordan";
mapView_.delegate=self;
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [UIImage imageNamed:#"car"];
marker.map = mapView_;
}
//[mapView_ setSelectedMarker:marker];
self.view = mapView_;
//
//
// CLLocation *locationA = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]];
//
// CLLocation *locationB = [[CLLocation alloc] initWithLatitude:[locLat doubleValue] longitude:[locLong doubleValue]];
//
// CLLocationDistance distance = [ locationA distanceFromLocation:locationB];
//
// NSLog (#"%f",distance);
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[self.locationManager startUpdatingLocation];

In your locationManager's delegate's method locationManager:didUpdateLocations: you can get your current location and it provides the method to calculate distanceFromLocation:(const CLLocation *)location
CLLocationDistance distA = [currentLocation distanceFromLocation: locationA];
CLLocationDistance distB = [currentLocation distanceFromLocation: locationB]; //etc

Related

My GMSMarker shows on top left , I want to show it on center objective-c

I want to show my marker on the center of the map but it shows on the top left.
Below is my code implementation:
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *userLoc=[kNSUserDefaults objectForKey:#"userLocation"];
mapView_.delegate = self;
CGFloat lat = [[userLoc objectForKey:#"lat"] floatValue];
CGFloat lon = [[userLoc objectForKey:#"long"] floatValue];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lat, lon);
mapView_.myLocationEnabled = YES;
mapView_.settings.myLocationButton = YES;
mapView_.mapType = kGMSTypeHybrid;
marker.map = mapView_;
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:lon zoom:17];
[mapView_ animateToCameraPosition:camera];
[mapView_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
mapView_.settings.compassButton = YES;
mapView_.mapType = kGMSTypeHybrid;
mapView_.settings.myLocationButton = YES;
mapView_.delegate = self;
mapView_.trafficEnabled = YES;
}
Not able to understand what's wrong in my code.
Thanks in advance!

Multiple marker is not showing

I want to show multiple marker on google map. There are answers based on this. But markers are not showing on the map. Although I am getting the latitude and longitude value based on the array result. What should I do?
Note: I have done some changes and the code running perfectly.
My code is:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self performRequestForRestaurantListing];
geometryDict=[[NSMutableDictionary alloc]init];
locationDict=[[NSMutableDictionary alloc]init];
NSLog(#"the value of list is %#", _service);
NSLog(#"the value of stringradius is %#", _stringRadius);
/*---location Manager Initialize-------*/
self.manager=[[CLLocationManager alloc]init];
self.manager.distanceFilter = 100;
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
[self.manager requestAlwaysAuthorization];
self.manager.delegate=self;
[self.manager startUpdatingLocation];
[mapView setDelegate:self];
latitude=#"22.5726";
longitude=#"88.3639";
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[latitude doubleValue]
longitude:[longitude doubleValue]
zoom:12];
[mapView animateToCameraPosition:camera];
[self coordinateOnMap:latitude andWithLongitude:longitude];
}
-(void)coordinateOnMap:(NSString*)latitude andWithLongitude:(NSString*)longitude
{
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
CLLocationCoordinate2D location;
for (int i=0;i<[restaurantList count];i++)
{
driverMarker = [[GMSMarker alloc] init];
latitude=[[[[restaurantList objectAtIndex:i]objectForKey:#"geometry"]objectForKey:#"location"] objectForKey:#"lat"];
longitude=[[[[restaurantList objectAtIndex:i]objectForKey:#"geometry"]objectForKey:#"location"] objectForKey:#"lng"];
location.latitude = [latitude floatValue];
location.longitude = [longitude floatValue];
driverMarker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
driverMarker.map = mapView;
}
driverMarker.icon=[UIImage imageNamed:#"marker"];
bounds = [bounds includingCoordinate:driverMarker.position];
driverMarker.title = #"My locations";
[driverMarker setTappable:NO];
mapView.myLocationEnabled = YES;
}
I guess your driveMarker gets deallocated by ARC immediatly after each loop.
If this really is your issue, you'll have to make sure that those markers "survive" the loop, e.g. with the following code:
#implementation MyController
#property (nonatomic) NSMutableArray *allMarkers;
- (void)viewDidLoad {
allMarkers = [[NSMutableArray alloc] init];
// ...
}
-(void)coordinateOnMap:(NSString*)latitude andWithLongitude:(NSString*)longitude {
//...
[allMarkers removeAllObjects];
for (int i=0;i<[restaurantList count];i++) {
GMSMarker *driverMarker = [[GMSMarker alloc] init];
[allMarkers addObject:driveMarker];
// ...
}
}
#end
This will create an NSArray property to store all created markers, just to keep them in scope.

I am trying to display multiple markers and a line between them on google map

for (int i=0; i<self.busRoutesArr.count-1; i++)
{
NSString *lat = [self.latArr objectAtIndex:i];
NSString *lon = [self.longArr objectAtIndex:i] ;
double lt=[lat doubleValue];
double ln=[lon doubleValue];
NSLog(#"%f, %f",lt,ln);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lt
longitude:ln
zoom:60];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectMake(10, 100, 250, 250) camera:camera];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.map = mapView;
mapView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mapView];
}
Only 1 marker being displayed. I want to display all the markers and a line connecting them
The following code is for displaying all markers in the map.
for(int i=0;i<[array count];i++) {
GMSMarker *marker = [[GMSMarker alloc] init];
marker.animated=YES;
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = #"name";
marker.snippet = #"snippet";
marker.map = mapView;
}

Google Maps iOS API markers not clearing or creating new markers

I have been working with the Google Maps API for iOS, and I am trying to recreate the positions of markers after getting an array of new positions/locations.
Here is my code for the Google Maps:
- (void)loadView {
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:locationManager.location.coordinate.latitude
longitude:locationManager.location.coordinate.longitude zoom:13.2];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.delegate = self;
self.view = mapView;
[mapView clear];
[self createMarker];
}
- (void) createMarker {
NSLog(#"createmarkers called");
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableArray *lat = [[NSMutableArray alloc]init];
NSMutableArray *lng = [[NSMutableArray alloc]init];
NSMutableArray *markers = [[NSMutableArray alloc]init];
NSMutableArray *businessArray = [[NSMutableArray alloc]init];
for (int x = 0; x < [appDelegate.businessArray count]; x++){
[businessArray addObject: [appDelegate.businessArray objectAtIndex:x] ];
}
for (int x = 0; x < [appDelegate.geocodedLatArrayGlobal count]; x++){
NSNumber *latCoord = [NSNumber numberWithDouble:[[appDelegate.geocodedLatArrayGlobal objectAtIndex:x]doubleValue]];
[lat addObject: latCoord];
}
for (int x = 0; x < [appDelegate.geocodedLngArrayGlobal count]; x++){
NSNumber *lngCoord = [NSNumber numberWithDouble:[[appDelegate.geocodedLngArrayGlobal objectAtIndex:x]doubleValue]];
[lng addObject:lngCoord];
}
for (int x = 0; x < [businessArray count]; x++){
double latitude =[[lat objectAtIndex:x]doubleValue];
double longitude =[[lng objectAtIndex:x]doubleValue];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude,longitude) ;
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = [[businessArray objectAtIndex:x]valueForKey:#"name"];
marker.snippet = #"123";
marker.icon = [UIImage imageNamed:#"greenPin.png"];
marker.animated = YES;
marker.map = mapView;
[markers addObject:marker];
}
}
If I load the view for the first time, the markers are being created. However, the second time with a new set/array of lat/long coordinates does not load.
The loadview method is being called, and the [mapView clear]; should be called, but it isn't clearing the markers on the mapview. The next problem is that the new markers are not being created, but I suspect that both these problems have to do with the same thing.
Any tips on why the map view won't clear? Thanks in advance

CoreLocation start Monitoring For multi Regions

how can i start monitoring for multi regions by using locationManager startMonitoringForRegion method
for example i have three regions i want to monitor it
CLLocationCoordinate2D centreLoc = {28.965243, 48.149724};
CLLocationDistance regionRadius = 200.00;
CLRegion *grRegion = [[CLRegion alloc] initCircularRegionWithCenter:centreLoc radius:regionRadius identifier:#"grRegion1"];
CLLocationCoordinate2D centreLoc2 = {28.765243, 48.149724};
CLLocationDistance regionRadius2 = 200.00;
CLRegion *grRegion2 = [[CLRegion alloc] initCircularRegionWithCenter:centreLoc2 radius:regionRadius2 identifier:#"grRegion2"];
CLLocationCoordinate2D centreLoc3 = {28.865243, 48.149724};
CLLocationDistance regionRadius3 = 200.00;
CLRegion *grRegion3 = [[CLRegion alloc] initCircularRegionWithCenter:centreLoc3 radius:regionRadius3 identifier:#"grRegion3"];
CLLocationAccuracy acc2=kCLLocationAccuracyBest;
[locationManager startMonitoringForRegion:grRegion2 desiredAccuracy:acc2];
how can start monitor for this three regions ???
You are on the right track.
First, your coordinates should be a double value, you are using a float value.
Second, you need to make separate arrays for Latitude and Longitude, and then take those values at index i.
This should work.
for (int i = 0; i < [AllRegionsArray count]; i++) {
NSArray *longLati = [allRegionsArray objectAtIndex:i];
NSMutableArray *latArray = longLati objectAtIndex:0];
NSMutableArray *longArray = longLati objectAtIndex:1];
lutiuid = [latArray objectAtIndex:i];
longtuid = [longArray objectAtIndex:i];
CLLocationCoordinate2D centreLoc = [CLLocation2DMake([[lutiuid] doubleValue], [[longtuid] doubleValue]);
//iOS 6
CLRegion *grRegion = [[CLRegion alloc] initWithCenter:centreLoc radius:200 identifier://yourIdentifier, I used the string of the address];
//iOS 7
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:coordinate radius:radius identifier:identifier];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startMonitoringForRegion:grRegion];
NSLog(#"monotoring regions:%#", locationManager.monitoredRegions);

Resources