GeoField, Lat/Long Coordinates - ios

I'm using Drupal's Geofield module to convert an address into latitude & longitude coordinates. In my iOS app, I'm trying to grab the coordinates it spits out with the following code, and place an annotation at the location on my mapview:
NSMutableDictionary *userData = [NSMutableDictionary new];
NSDictionary *pins = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:userData, nil] forKeys:[NSArray arrayWithObjects:#"und", nil]];
NSDictionary *morepins = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:pins] forKey:#"field_position"];
NSLog(#"%#", userData);
NSLog(#"%#", morepins);
CLLocationCoordinate2D coord = {[[morepins objectForKey:#"lat"] doubleValue], [[morepins objectForKey:#"lon"] doubleValue]};
mapPin *ann = [[mapPin alloc] init];
ann.title = #"You are here";
ann.subtitle = #"Let's go!";
ann.coordinate = coord;
[mapView addAnnotation:ann];
However when I log userData and morepins, both are empty. I'm using the Drupal iOS SDK - does anyone know what I'm doing wrong here? I'm trying to grab the lat and lon fields. Here is how Drupal is displaying the data:

Related

Show multiple map annotations/coordinates on mapview

I'm using the below code to display addresses from an array (responseObject) as annotations on my mapview. It works, and the pin is dropped successfully from my location string, however it only shows a pin for the most recent address added to the array. How can I change my code so that it shows pins on the map for all addresses in my array instead of just the most recent one? Apologies if this is a newb question. Thanks!
viewcontroller.m
NSMutableDictionary *viewParams = [NSMutableDictionary new];
[viewParams setValue:#"u000" forKey:#"view_name"];
[DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.addressData = [responseObject mutableCopy];
NSString *location = self.addressData[0][#"address"];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location
completionHandler:^(NSArray* placemarks, NSError* error){
if (placemarks && placemarks.count > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKCoordinateRegion region = self.mapView.region;
// region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:placemark];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = self.addressData[0][#"users_name"];
point.subtitle = self.addressData[0][#"userbio"];
[self.mapView addAnnotation:point];
You are accessing only one object ?
NSString *location = self.addressData[0][#"address"];
Edited
I think you should handle your data, separated with your view. i.e. implement geocoder related code in the mapView:viewForAnnotation: method in your map view delegate. Then you should be able to create the annotations one by one and use [self.mapView addAnnotations] for all of them
For your code, which I believe is inspired by this answer, you should be able to iterate through all location addresses by something like
for (NSMutableDictionary *loc in self.addressData) {
NSString *loc = location[#"address"];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
......
}
Forgive me if the syntax is wrong for Objective C.

How to create route map using the google maps sdk for ios

Hi in my application i have to give route to my destination coordinates so i have integrated google map in my application but i dont how to give route navigation for the that. After long search i got some sample code. Its like if tape locations in map it will show the route direction on the map.
This is my smaple code.
(void)loadView {
waypoints_ = [[NSMutableArray alloc]init];
waypointStrings_ = [[NSMutableArray alloc]init];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:12.9259
longitude:77.6229
zoom:13];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.delegate = self;
self.view = mapView_;
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:
(CLLocationCoordinate2D)coordinate {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
coordinate.latitude,
coordinate.longitude);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.map = mapView_;
[waypoints_ addObject:marker];
NSString *positionString = [[NSString alloc] initWithFormat:#"%f,%f",
coordinate.latitude,coordinate.longitude];
[waypointStrings_ addObject:positionString];
if([waypoints_ count]>1){
NSString *sensor = #"false";
NSArray *parameters = [NSArray arrayWithObjects:sensor, waypointStrings_,
nil];
NSArray *keys = [NSArray arrayWithObjects:#"sensor", #"waypoints", nil];
NSDictionary *query = [NSDictionary dictionaryWithObjects:parameters
forKeys:keys];
MDDirectionService *mds=[[MDDirectionService alloc] init];
SEL selector = #selector(addDirections:);
[mds setDirectionsQuery:query
withSelector:selector
withDelegate:self];
}
}
- (void)addDirections:(NSDictionary *)json {
NSDictionary *routes = [json objectForKey:#"routes"][0];
NSDictionary *route = [routes objectForKey:#"overview_polyline"];
NSString *overview_route = [route objectForKey:#"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = mapView_;
}
The above code will show the route location when u tape on the map i want to do like my current location to my destination coordinate please tell me how to achieve this one.
for example this my destination coordinate 12.9259,77.6229
Thanks.
All you need to do is just select two points and give it too google to give you a route's JSON and it will draw it on your map. This is what I did which draw a route from device's location to your destination point :
-(void) updateRoute {
self.isMapUpdated = YES;
CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(
self.locationManager.location.coordinate.latitude,
self.locationManager.location.coordinate.longitude);
GMSMarker *marker2 = [GMSMarker markerWithPosition:position2];
marker2.map = self.mapView;
marker2.title = #"I AM HERE!!";
[waypoints_ addObject:marker2];
NSString *positionString2 = [[NSString alloc] initWithFormat:#"%f,%f",
self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude];
[waypointStrings_ addObject:positionString2];
CLLocationCoordinate2D position1 = CLLocationCoordinate2DMake( destination.LAT, destination.LONG);
GMSMarker *marker1 = [GMSMarker markerWithPosition:position1];
marker1.map = self.mapView;
marker1.icon = [UIImage imageNamed:#"marker.png"];
[waypoints_ addObject:marker1];
NSString *positionString1 = [[NSString alloc] initWithFormat:#"%f,%f",
destination.LAT,destination.LONG];
[waypointStrings_ addObject:positionString1];
if([waypoints_ count]>1){
NSString *sensor = #"false";
NSArray *parameters = [NSArray arrayWithObjects:sensor, waypointStrings_,
nil];
NSArray *keys = [NSArray arrayWithObjects:#"sensor", #"waypoints", nil];
NSDictionary *query = [NSDictionary dictionaryWithObjects:parameters
forKeys:keys];
MDDirectionService *mds=[[MDDirectionService alloc] init];
SEL selector = #selector(addDirections:);
[mds setDirectionsQuery:query
withSelector:selector
withDelegate:self];
}
}
- (void)addDirections:(NSDictionary *)json {
if([json objectForKey:#"routes"] != nil)
if([[json objectForKey:#"routes"] count] != 0){
NSDictionary *routes = [json objectForKey:#"routes"][0];
NSDictionary *route = [routes objectForKey:#"overview_polyline"];
NSString *overview_route = [route objectForKey:#"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [Utility colorWithHexString:#"790566"];
polyline.strokeWidth = 3.0;
polyline.map = self.mapView;
}
}
Also you need MDDirectionService header and main files to import to your project and add the header file to your class which you can download them from the simple app which Google provided it.

Find nearest annotations from user location ios?

I have an array of annotations.I am calculating the distance between each annotation from user location.What I want to know is, how can I find lets say 3 nearest annotations from these distances?
Here is how I am calculating distance from user location to annotations.
CLLocation *userLocation = self.mapView.userLocation.location;
for (Annotation *obj in annotations) {
CLLocation *loc = [[CLLocation alloc] initWithLatitude:obj.coordinate.latitude longitude:obj.coordinate.longitude];
// CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:self.mapView.userLocation.coordinate.latitude longitude:self.mapView.userLocation.coordinate.longitude];
CLLocationDistance dist = [loc distanceFromLocation:userLocation];
int distance = dist;
NSLog(#"Distance from Annotations - %i", distance);
}
The userLocation is static, so don't recreate it each time in your loop. You can also get the location directly with:
CLLocation *userLocation = self.mapView.userLocation.location;
Then, you could do something like, store your distance numbers and annotations into a dictionary, with the distances as keys and the annotations as the values. Once the dictionary is built, sort the allKeys array and then you can get the annotations associated with the first 3 keys.
Assuming that you always have at least one annotation when you run this:
CLLocation *userLocation = self.mapView.userLocation.location;
NSMutableDictionary *distances = [NSMutableDictionary dictionary];
for (Annotation *obj in annotations) {
CLLocation *loc = [[CLLocation alloc] initWithLatitude:obj.coordinate.latitude longitude:obj.coordinate.longitude];
CLLocationDistance distance = [loc distanceFromLocation:userLocation];
NSLog(#"Distance from Annotations - %f", distance);
[distances setObject:obj forKey:#( distance )];
}
NSArray *sortedKeys = [[distances allKeys] sortedArrayUsingSelector:#selector(compare:)];
NSArray *closestKeys = [sortedKeys subarrayWithRange:NSMakeRange(0, MIN(3, sortedKeys.count))];
NSArray *closestAnnotations = [distances objectsForKeys:closestKeys notFoundMarker:[NSNull null]];
NSArray *numbers = [NSArray arrayWithObjects:
[NSNumber numberWithInt:0],
[NSNumber numberWithInt:3],
[NSNumber numberWithInt:2],
[NSNumber numberWithInt:8],
nil];
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:#selector(localizedCompare:)];
NSArray *sortedNumbers = [numbers sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
you store all distance in an array and sort it and then pick up top there number
[sortedNumbers objectAtIndex:0];
[sortedNumbers objectAtIndex:1];
[sortedNumbers objectAtIndex:2];

Google maps in iOS doesn't show multiple pins

I am using Google Maps SDK in iOS app.I am stuck at plotting multiple pins on map.This is how I am trying to plot the pins.And I used the exact same approach to show multiple pins using MapKit which worked fine.But no success with google Maps.
-(void)plotMembersOnMap
{
for (NSMutableDictionary *obj in self.jsonDictionary)
{
membersDict = [obj objectForKey:#"members"];
NSLog(#"Count member %d",[membersDict count]); // shows count
for (NSDictionary *obj in membersDict)
{
CLLocationCoordinate2D center;
NSString *latitudeString = [obj objectForKey:#"lat"];
NSString *longitudeString = [obj objectForKey:#"lng"];
double latitude = [latitudeString doubleValue];
double longitude = [longitudeString doubleValue];
center.latitude =latitude;
center.longitude = longitude;
NSString *userName = [obj objectForKey:#"pseudo"];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.map = mapView_;
marker.position = CLLocationCoordinate2DMake(center.latitude, center.longitude);
customGoogleCallout.callOutTitleLabel.text = #"Member";
customGoogleCallout.callOutUserName.text = userName;
marker.icon = [UIImage imageNamed:#"marker_membre.png"];
}
}
}
just try to add lat and lon to an array then supply the marker.position.
have some loop for i and position is object at index[i].
this might help...
CLLocationCoordinate2D center = { [[obj objectForKey:#"lat"] floatValue] , [[obj objectForKey:#"lon"] floatValue] };
GMSMarker *marker = [GMSMarker markerWithPosition:center];
Try this ,
NSArray *latitudeString = [obj objectForKey:#"lat"];
NSArray *longitudeString = [obj objectForKey:#"lng"];
Instead of
NSString *latitudeString = [obj objectForKey:#"lat"];
NSString *longitudeString = [obj objectForKey:#"lng"];
Maybe because it's plotting the latest coordinate you assign to center. You should make new instance so the previous value will not replaced.

MKPointAnnotation Expression is not assignable

So I have the following code block, which is supposed to iterate over an array of JSON objects and place MKPointAnnotations on a map:
for(id jsonObject in dataArray)
{
NSLog(#"%d",[dataArray count]);
NSDictionary* jsonDictionary = jsonObject;
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
NSString *lat = [jsonDictionary objectForKey:#"latitude"];
NSString *lon = [jsonDictionary objectForKey:#"longitude"];
point.coordinate.latitude = [lat doubleValue];
point.coordinate.longitude = [lon doubleValue];
[map addAnnotation:point];
}
However, the two lines:
point.coordinate.latitude = [lat doubleValue];
point.coordinate.longitude = [lon doubleValue];
are giving me an "Expression is not Assignable" error. I can't for the life of me figure it out. I've tried to make a CLLocationCoordinate2D object and assigning that, but it doesn't work either.
This should work:
CLLocationCoordinate2d coordinate = ...
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
[mapView addAnnotation:annotation];
It works in an existing app, just checked the code and the app.
Check this answer as well: https://stackoverflow.com/a/15162092/1032151

Resources