How to Load Latitude longitude from plist? - iphone

i want to add latitude and longitude in plist and want to show the location on map view.
I am unable to load latitude and longitude in plist.
NSString *path = [[NSBundle mainBundle] pathForResource:#"map" ofType:#"plist"];
NSArray *anns = [NSArray arrayWithContentsOfFile:path];
NSLog(#"anns=%#",anns);
for(NSMutableDictionary *note in anns) {
float realLatitude = [[note objectForKey:#"latitude"] floatValue];
float realLongitude = [[note objectForKey:#"logitude"] floatValue];
WHLocationAnnotation* myAnnotation = [[WHLocationAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
NSLog(#"...abc...%f",[[note objectForKey:#"latitude"] floatValue]);
myAnnotation.title = [note objectForKey:#"Title"];
myAnnotation.subtitle = [note objectForKey:#"subtitle"];
[mapViewForAnnotation setDelegate:self];
[mapViewForAnnotation addAnnotation:myAnnotation];
}
}
I have done this in viewdidload. I am getting latitude longitude with nslog but location is not correct on mapview.

Related

how to move in map of iphone simulator ? dynamic Latitude & Longitude getting but not locate in iphone simulator?

-(CLLocationCoordinate2D)getGeoCoding:(NSString *)address
{
NSString *req = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%#",address];
NSURL *url = [NSURL URLWithString:req];
NSError *error;
//NSURLResponse *response;
CLLocationCoordinate2D findlocation;
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
NSMutableDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:Nil error:&error];
if(error)
{
NSLog(#"%#",[error localizedDescription]);
findlocation.latitude=0.0;
findlocation.longitude =0.0;
}
else
{
NSArray *results = (NSArray *)responseDictionary[#"results"];
NSDictionary *firstitem = (NSDictionary *)[results objectAtIndex:0];
NSLog(#"%#",firstitem);
NSDictionary *geometry = (NSDictionary *)[firstitem valueForKey:#"geometry"];
NSLog(#"%#",geometry);
NSDictionary *location = (NSDictionary *)[geometry valueForKey:#"location"];
NSLog(#"%#",location);
NSNumber *lat = (NSNumber *)[location valueForKey:#"lat"];
NSLog(#"%#",lat);
NSNumber *lng = (NSNumber *)[location valueForKey:#"lng"];
NSLog(#"%#",lng);
findlocation.latitude = [lat doubleValue];
findlocation.longitude = [lng doubleValue];
}
return findlocation;
}
i getting location from above code.
-(void)getLocation:(id)sender
{
NSString *str = self.textView.text;
CLLocationCoordinate2D location;
location=[self getGeoCoding:str];
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.mapView.showsUserLocation =YES;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location, 0.3*METERS_PER_MILES, 0.3*METERS_PER_MILES);
[self.locationManager startUpdatingLocation];
[self.mapView reloadInputViews];
[self.mapView setRegion:viewRegion animated:YES];
}
location code is getting after the above function.
function is called but that do not work in iPhone simulator..?
if the set any setting Please tell me what the problem in code because iPhone simulator map not move from current location..
Is it Possible? How?

MKMapkit route is misaligned or not in the centre of road

I am developing an application in which I need to draw a route between two points using lat/long.
I have used apple API to get polylines and drawing it after decoding it.
Problem:
Route is not in the middle of the road (Attached image_1) or route is misaligned
Below is the code:
NSString* saddr = [NSString stringWithFormat:#"%#,%#",self.lat1.text,self.long1.text];
NSString* daddr = [NSString stringWithFormat:#"%#,%#",self.lat2.text,self.long2.text];
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.apple.com/maps/api/directions/json?origin=%#&destination=%#&sensor=false", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSError *error;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
NSData *responseData = [apiResponse dataUsingEncoding:NSUTF8StringEncoding];
NSError* error1;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&error1];
NSLog(#"Error: %#\n%#",[error1 localizedDescription],[error1 localizedFailureReason]);
if([[json objectForKey:#"status"] isEqualToString:#"OK"])
{
NSArray *routes1 = [json objectForKey:#"routes"];
NSDictionary *route = [routes1 lastObject];
if (route)
{
NSString *overviewPolyline = [[route objectForKey: #"overview_polyline"] objectForKey:#"points"];
routes = [self decodePolyLine:overviewPolyline];
//NSLog(#"%#",[routes objectAtIndex:0]);
[self updateRouteView];
[self centerMap];
}
}
-(void) updateRouteView
{
NSInteger numberOfSteps = routes.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [routes objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
decodePolyLine is totally depend n the latitude and longitude you get. So if you get proper latitude and longitude then this case will not happen. Another reason for that tis from iOS6 and above apple is using it's own map so may be they have different latitude and longitude from the Google map.
It's the same problem you described in your other question and it has the same solution. You are using data from two different sources, and the data is different.

Multiple Pins on Map using data from Plist

I am trying to show multiple pins on a map using latitude and longitude from an array of dictionaries. The problem is it is only showing the pin for the last dictionary in the plist always.
Here is the method I have:
- (void)loadMapPins
{
MapAnnotation *annotation = [[MapAnnotation alloc] init];
for (int i=0; i<self.dataDictionary.count; i++){
NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[self.dataDictionary objectAtIndex:i]];
double latitude = [[dictionary objectForKey:#"Latitude"] doubleValue];
double longitude = [[dictionary objectForKey:#"Longitude"] doubleValue];
CLLocationCoordinate2D coord = {.latitude =
latitude, .longitude = longitude};
MKCoordinateRegion region = {coord};
annotation.title = [dictionary objectForKey:#"Name"];
annotation.subtitle = [dictionary objectForKey:#"Center Type"];
annotation.coordinate = region.center;
[mapView addAnnotation:annotation];
}
}
I need it to go through the loop and drop the pins on the map accordingly. Any help/examples are appreciated.
I think you want to move your annotation creation into your loop. From the looks of things you only created one, then in the loop you mutate it over and over again. When the loop completes, the last modification to your annotation variable reflects the last item in self.dataDictionary that you are iterating.
The code below creates a new annotation object each loop iteration.
- (void)loadMapPins
{
for (int i=0; i<self.dataDictionary.count; i++){
NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:[self.dataDictionary objectAtIndex:i]];
double latitude = [[dictionary objectForKey:#"Latitude"] doubleValue];
double longitude = [[dictionary objectForKey:#"Longitude"] doubleValue];
CLLocationCoordinate2D coord = {.latitude =
latitude, .longitude = longitude};
MKCoordinateRegion region = {coord};
MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.title = [dictionary objectForKey:#"Name"];
annotation.subtitle = [dictionary objectForKey:#"Center Type"];
annotation.coordinate = region.center;
[mapView addAnnotation:annotation];
}
}
Hope this helps,
Scott H

Objc_msgSend and app crash

When I trying to launch my app on simulator (3.1.3 , Xcode 3.1.4), it shows me objc_msgSend and application does not launch. This happens only when I alloc NSMUtable Array
This is my code part,
in viewDidLoad,
-(void)viewDidLoad{
locationArray = [[NSMutableArray alloc] initWithObjects:#"new delhi", #"faridabad",#"meerut"];
str1=[locationArray objectAtIndex:0];
str2=[locationArray objectAtIndex:1];
[super viewDidLoad];
}
Then I want to use locationArray objects in following method;
-(CLLocationCoordinate2D) addressLocation1{
double latitude = 0.0;
double longitude = 0.0;
NSString *str= [locationArray NSString *str= [locationArray objectAtIndex:0];
//NSString *str= #"new delhi"
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
problem occur only when alloc the locationArray, whats wrong with me, please help
thanks
locationArray = [[NSMutableArray alloc] initWithObjects:#"new delhi", #"faridabad",#"meerut"];
Try
locationArray = [[NSMutableArray alloc] initWithObjects:#"new delhi", #"faridabad",#"meerut", nil];
From The NSArray docs you see that for initWithObjects: it requires termination with nil. If you don't want to do that, and you know how many you have, you can use initWithObjects:count:

Speed up addAnnotations from Google Geocoding?

I have the following code in my app which adds a number of Annotations to a mapview via Google geocoding:
for (PlaceObject *info in mapLocations) {
NSString * addressOne = info.addressOne;
NSString * name = info.name;
NSString * address = [addressOne stringByAppendingString:#",London"];
NSError * error;
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
} else {
//Error handling
}
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];
[mapViewLink addAnnotation:annotation];
}
This works, however it takes a long time for the view to appear as it seems to wait until all objects in the array have been looped through (there are about 80).
Is there any way to make the view load and then for the pins to be added as they are created ?
How about this:
for (PlaceObject *info in mapLocations) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// GET ANNOTATION INFOS
NSString * addressOne = info.addressOne;
NSString * name = info.name;
NSString * address = [addressOne stringByAppendingString:#",London"];
NSError * error;
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
} else {
//Error handling
}
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitude;
coordinate.longitude = longitude;
MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];
dispatch_sync(dispatch_get_main_queue(), ^{
// ADD ANNOTATION
[mapViewLink addAnnotation:annotation];
});
});
}
But please consider to double-check this code. There are definitily things you should do about keeping the threads safe and avoid EXC_BAD_ACCESS.
Where is this code executed in your app?
I'd recommend using NSURLConnection and adding the annotations after the data has been received and parsed. Look here for more info, if you're not familiar with NSURLConnection: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
Yea, you are making the classic error of doing long work (net loading from a URL) in the main (UI) thread. Move this work into a background thread and then have the main thread add the pins. This can be in several ways, by creating another explicit thread, GCD with blocks, or just performSelector in background thread. Easiest way is to probably performSelector in background and then when it has something to add to map, performSelector on main thread.