This should be Los Angeles:
double latitude = 34.05;
double longitude = 118.25;
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude, longitude);
NSString *desc = #"hey there";
NSString *address = #"some address";
CLLocationCoordinate2D coordinate;
coordinate.latitude = location.latitude;
coordinate.longitude = location.longitude;
MyLocation *annotation = [[MyLocation alloc] initWithName:desc address:address coordinate:coordinate]; //implements <MKAnnotation>
NSArray *annotations = #[annotation];
[self.mapView showAnnotations:annotations animated:YES];
However this annotation ends up in Eastern China. What am I missing in the conversion?
I think you want -118.25 degrees for Los Angles.
Related
I am using google map SDKs in my iOS project to track the user location and a draw a route line of the user path for his movement.I want to add two images for the starting point of the user and another for user movement. I can't add these images.Please help me to add these images. Here is my code:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//get the latest location
CLLocation *currentLocation = [locations lastObject];
//store latest location in stored track array;
[self.locations addObject:currentLocation];
//get latest location coordinates
CLLocationDegrees Latitude = currentLocation.coordinate.latitude;
CLLocationDegrees Longitude = currentLocation.coordinate.longitude;
CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude);
//zoom map to show users location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 1000,1000);
MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion];
[mapview setRegion:adjustedRegion animated:YES];
NSInteger numberOfSteps = self.locations.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [self.locations objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
coordinates[index] = coordinate2;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[mapview addOverlay:polyLine];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 10.0;
return polylineView;
}
This code draw the user path. But I want this type of output.
Someone please help me.
Add annotations to mapview, for first and last objects of your self.locations array. You will also need to remove the old annotation each time the location is updated to a new one.
Let centerY and centerX be the center point for the image
CLLocationDegrees offset = 0.01; // change size here
CLLocationDegrees southWestY = centerY - offset;
CLLocationDegrees southWestX = centerX - offset;
CLLocationDegrees northEastY = centerY + offset;
CLLocationDegrees northEastX = centerX + offset;
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestY,southWestX);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastY,northEastX);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest coordinate:northEast];
GMSGroundOverlay *overlay = [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:[UIImage imagedNamed:#"YourImage.png"]];
overlay.bearing = 0;
overlay.map = self.mapView
With this approach (unlike with the marker) the image will zoom in and out as the user zooms in/out of the map.
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.
In my app, I have done JSON parsing and I got the coordinates in the form of a dictionary, I want to use the coordinates angd plot it in the map,
I have using this
SBJsonParser *jsonParser = [SBJsonParser new];
NSArray *jsonData = (NSArray *) [jsonParser objectWithString:outputData error:nil];
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
Nslog(#"%#",dict);
double la=[[dict objectForKey:#"latitude"] doubleValue];
double lo=[[dict objectForKey:#"longitude"] doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
}
it is printed as
[{"driver_id":"Tn1234sunil#gmail.com","username":"sunil","latitude":"0.000000000000000",
"longitude":"0.000000000000000"},
{"driver_id":"ma12marii#yahoo.com","username":"mari","latitude":"13.040720500000000",
"longitude":"80.243139600000000"}, {"driver_id":"45sabala#gmail.com","username":"balaji","latitude":"0.000000000000000",
"longitude":"0.000000000000000"}
Then, In my log, it is getting printed as
2014-01-04 10:55:48.121 MyTaxi[608:12e03] latitude : 0.000000
2014-01-04 10:55:48.121 MyTaxi[608:12e03] longitude : 0.000000
2014-01-04 10:55:48.122 MyTaxi[608:12e03] latitude : 13.040721
2014-01-04 10:55:48.122 MyTaxi[608:12e03] longitude : 80.243140
2014-01-04 10:55:48.122 MyTaxi[608:12e03] latitude : 0.000000
2014-01-04 10:55:48.123 MyTaxi[608:12e03] longitude : 0.000000
But, this doesnt works properly
Does any body have an idea how to plot these points to the google maps
try this one this might be helpful just create a for loop to your count ,increment it ...
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
double la=[[dict valueForKey:#"latitude"] doubleValue];
double lo=[[dict valueForKey:#"longitude"] doubleValue];
NSMutableArray * latArray=[[NSMutableArray alloc]init];
NSMutableArray * longArray=[[NSMutableArray alloc]init];
[latArray addObject:[NSNumber numberWithDouble:la]];
[longArray addObject:[NSNumber numberWithDouble:lo]];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:[[latArray objectAtIndex:i]doubleValue] longitude:[[longArray objectAtIndex:i]doubleValue]];
CLLocationCoordinate2D coordi=loca.coordinate;
GMSMarker *marker= [[GMSMarker alloc] init];
marker=[GMSMarker markerWithPosition:coordi];
marker.position = CLLocationCoordinate2DMake([[latArray objectAtIndex:i]doubleValue], [[longArray objectAtIndex:i]doubleValue]);
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
I think , You have to alloc Marker in for loop,
right now you are creating only one marker,
for(int i=0;i<[jsonData count];i++)
{
NSDictionary *dict=(NSDictionary *)[jsonData objectAtIndex:i];
double la=[dict valueForKey:#"latitude" doubleValue];
double lo=[dict valueForKey:#"longitude" doubleValue];
CLLocation * loca=[[CLLocation alloc]initWithLatitude:la longitude:lo];
CLLocationCoordinate2D coordi=loca.coordinate;
GMSMarker *marker= [[GMSMarker alloc] init];
marker=[GMSMarker markerWithPosition:coordi];
marker.snippet = #"Hello World";
marker.animated = YES;
marker.map = mapView;
.....
}
I am adding annotations and there are supposed to be 5 annotation pins on my map but only 2 appear and this error is shown:
An instance 0x9d8e720 of class Annotation was deallocated while key
value observers were still registered with it. Observation info was
leaked, and may even become mistakenly attached to some other object.
Set a breakpoint on NSKVODeallocateBreak to stop h
//NUH COORDINATES
#define NUH_latitude 1.293700;
#define NUH_longitude 103.783353;
//span
#define the_span 0.002f;
//NHGP COORDINATES!
#define NHGP_latitude 1.295938;
#define NHGP_longitude 103.782857;
//NUHER COORDINATES
#define NUHER_latitude 1.294968;
#define NUHER_longitude 103.783716;
//NUCIS COORDINATES
#define NUCIS_latitude 1.293149;
#define NUCIS_longitude 103.783464;
//Viva-UCCC COORDINATES!
#define VivaUCCC_latitude 1.294099;
#define VivaUCCC_longitude 103.783233;
//span
MKCoordinateSpan span;
span.latitudeDelta = the_span;
span.longitudeDelta = the_span;
MKCoordinateRegion myRegion;
//center
CLLocationCoordinate2D center;
center.latitude = NUH_latitude;
center.longitude = NUH_longitude;
myRegion.center =center;
myRegion.span = span;
[mapview setRegion:myRegion animated:YES];
//annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//NUH location
myAnn = [[Annotation alloc] init];
location.latitude = NUH_longitude;
location.longitude = NUH_longitude;
myAnn.coordinate = location;
myAnn.title = #"NUH";
[locations addObject:myAnn];
//NHGP location
myAnn = [[Annotation alloc] init];
location.latitude = NHGP_latitude;
location.longitude = NHGP_longitude;
myAnn.coordinate = location;
myAnn.title = #"National Healthcare Group Polyclinic ";
[locations addObject:myAnn];
//NUHER location
myAnn = [[Annotation alloc] init];
location.latitude = NUHER_longitude;
location.longitude = NUHER_longitude;
myAnn.coordinate = location;
myAnn.title = #"National University Hospital Emergency Room";
[locations addObject:myAnn];
//NUCIS location
myAnn = [[Annotation alloc] init];
location.latitude = NUCIS_longitude;
location.longitude = NUCIS_longitude;
myAnn.coordinate = location;
myAnn.title = #"National University Cancer Institute Singapore";
[locations addObject:myAnn];
//Viva-UCCC location
myAnn = [[Annotation alloc] init];
location.latitude = VivaUCCC_latitude;
location.longitude = VivaUCCC_longitude;
myAnn.coordinate = location;
myAnn.title = #"Viva-University Children's Cancer Centre";
[locations addObject:myAnn];
[self.mapview addAnnotations:locations];
You've specified longitudes for the latitudes for NUH, NUHER, and NUCIS. They won't show up where you intended (which is why you only see 2), and I think the error is because those values are not valid for latitudes.
//NUH location
location.latitude = NUH_longitude; // <-- should be NUH_latitude
...
//NUHER location
location.latitude = NUHER_longitude; // <-- should be NUHER_latitude
...
//NUCIS location
location.latitude = NUCIS_longitude; // <-- should be NUCIS_latitude
It could be because you keep reallocting the same myAnn variable. You should try creating multiple Annotations, call them myAnn1, myAnn2 if you have to.
Later on if you have more than 3 of these things you should consider making a collection and looping through it rather than creating them one-by-one
I am using this function to get the distance from one CLLocation to another
here venue.lat is NSNumber
GeoAddress *address = [[GeoAddress new] init];
CLLocationCoordinate2D coord;
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
address.latlng = [[CLLocation alloc] initWithLatitude:coord.latitude longitude: coord.longitude];
if (curUserLocation.coordinate.latitude){
address.distance = [address.latlng distanceFromLocation:curUserLocation];
NSLog(#" DISTANCE %f", address.distance);
NSLog(#" LOC1 %f lat %f lng", address.latlng.coordinate.latitude, address.latlng.coordinate.longitude);
NSLog(#" ME %f lat %f lng", curUserLocation.coordinate.latitude, curUserLocation.coordinate.longitude);
}
This is Geoaddress:
// GeoAddress.h
#interface GeoAddress : NSObject
{
CLLocation * latlng;
CLLocationDistance distance;
}
#property CLLocationDistance distance;
#property (nonatomic, retain) CLLocation * latlng;
// GeoAddress.m
#import "GeoAddress.h"
#implementation GeoAddress
#synthesize distance;
#synthesize latlng;
This is the log:
DISTANCE 15106456.105786
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng
The numbers are completely off actual distance is ~0.17 km
What am I doing wrong??
You have lines that say:
coord.longitude = (CLLocationDegrees)[venue.lat doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lng doubleValue];
Obviously that should be:
coord.longitude = (CLLocationDegrees)[venue.lng doubleValue];
coord.latitude = (CLLocationDegrees)[venue.lat doubleValue];
Where you set coord from venue, the latitude and longitude are being assigned backwards.
This gives you the unexpected distance.
Look at your output:
LOC1 -73.986357 lat 40.702645 lng
ME 40.702082 lat -73.984775 lng
You exchanged lat and long in ME