3
votes

I am developing an iPhone application that works with maps. In this app, I need to show some parking places (addresses) based on their latitude and longitude. At this point, I finished successfully, but I would like to show the Pin Points relative to the user's direction(SE,SW,NE,NW). I tried using compass mode in MKMapview, but it shows the pin points in the wrong directions.

Is there a mistake in my code or is it the web service's fault? I am passing the user location LAT and LONG and user direction to web service.

I am showing you the screen shots now.enter image description here

enter image description here

- (void)updateHeading:(CLHeading *) newHeading {
NSLog(@"New magnetic heading: %f", newHeading.magneticHeading);
NSLog(@"New true heading: %f", newHeading.trueHeading);
double rotation = newHeading.magneticHeading * 3.14159 / 180;
NSLog(@"rotation ========> %f",rotation);
[mapView setTransform:CGAffineTransformMakeRotation(rotation)];

[[mapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    MKAnnotationView * view = [mapView viewForAnnotation:obj];

    [view setTransform:CGAffineTransformMakeRotation(rotation)];
}];
//NSLog(@"newlocation course : %f",mapView.userLocation.location.course);
float mHeading = newHeading.magneticHeading;
NSLog(@"mHeading %f",mHeading);
//direction is a string
if(mHeading > 0 && mHeading <90) {
    // north east
    NSLog(@"~~~~~~~~~~~~~~~~north east");
    direction = @"North East";
}
else if(mHeading > 90 && mHeading < 180)  {
    // south east
    NSLog(@"~~~~~~~~~~~~~~~~south east");
    direction = @"South East";
}
else if(mHeading > 180 && mHeading < 270) {
    // south west
    NSLog(@"~~~~~~~~~~~~~~~~south west");
    direction = @"South West";
}
else if(mHeading > 270 && mHeading < 360) {
    // north west
    NSLog(@"~~~~~~~~~~~~~~~~north west");
    direction = @"North West";
}

if([changedDirction isEqualToString:direction])
{
    NSLog(@"called nothing/changedDirction.....");
}
else
{
  NSURL *url = [NSURL URLWithString:@"http://www.charlottecentercity.org/services/nearbylocations.php"];
  NSLog(@"url is---%@",url);
  directionsRequest = [[ASIFormDataRequest alloc] initWithURL:url];
 [directionsRequest setPostValue:[NSString stringWithFormat:@"%f", appdelegate.currentLocation.latitude] forKey:@"lat"];
 [directionsRequest setPostValue:[NSString stringWithFormat:@"%f", appdelegate.currentLocation.longitude] forKey:@"long"];
 directionLabel.text = direction;
[directionsRequest setPostValue:direction forKey:@"direction"];
[directionsRequest setPostValue:self.mapID forKey:@"id"];
 directionsRequest.delegate = self;
[directionsRequest startAsynchronous];
 changedDirction = direction;
 } }


-(void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"request finished is calling....");
if ([venuDescArray count]>0)
{
    [venuDescArray removeAllObjects]; 

}

NSError *error = [request error];
NSLog(@"error is%@",error);
if (!error) 
{
    NSString *responseString = [request responseString];
    NSMutableArray *array = [responseString JSONValue];
    NSLog(@"response array for category wise pin--%@",array);
    for (int i = 0; i<[array count]; i++) 
    {
        NSMutableDictionary *theDict = [array objectAtIndex:i];
        PlacesObject *aVenue = [[PlacesObject alloc] init];
        aVenue.venueDetailsplaceID = [theDict objectForKey:@"placeid"];
        aVenue.venueDetailsname = [theDict objectForKey:@"name"];
        aVenue.venueDetailsaddress = [theDict objectForKey:@"address"];
        aVenue.venueDetailslatitude = [theDict objectForKey:@"latitude"];
        aVenue.venueDetailslongitude = [theDict objectForKey:@"longitude"];

        [venuDescArray addObject:aVenue];
    }

    NSLog(@"venuDescArray count is---%d",[venuDescArray count]);
}
[self setLocations]; }

-(void)setLocations {

[annotaionsArray11 removeAllObjects];
[mapView removeAnnotations:mapView.annotations];
 NSLog(@"annotations removed....");

self.mapView.mapType = MKMapTypeStandard;
self.mapView.delegate = self;

MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];

float lati_new =appdelegate.currentLocation.latitude;//35.227087;
float longi_new =appdelegate.currentLocation.longitude;//-80.843127; 

myAnnotation.annType=@"Current";
myAnnotation.title1 =@"Current our Location";

myAnnotation.latitude=[NSNumber numberWithFloat:lati_new];
myAnnotation.longitude =[NSNumber numberWithFloat:longi_new];
NSLog(@"myAnnotation.latitude/float---%@",myAnnotation.latitude);
NSLog(@"myAnnotation.longitude/float---%@",myAnnotation.longitude);

[annotaionsArray11 addObject:myAnnotation];

[myAnnotation setButtonTag:0];

 NSLog(@"self.venuDescArray count:%d",[self.venuDescArray count]);

PlacesObject *placesObj=[[PlacesObject alloc] init];

for (int i=0; i<[self.venuDescArray count]; i++) 
{
    placesObj=[self.venuDescArray objectAtIndex:i];

    MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];

    myAnnotation.annType=@"Points";

    myAnnotation.title1 =placesObj.venueDetailsname;
    NSLog(@"myAnnotation.title1's is---%@",myAnnotation.title1);
    myAnnotation.subTitle =placesObj.venueDetailsaddress;
    NSLog(@"myAnnotation.subTitle is---%@",myAnnotation.subTitle);
    myAnnotation.latitude =[NSNumber numberWithFloat:[placesObj.venueDetailslatitude floatValue]];
    NSLog(@"myAnnotation.latitudes is ----%@",myAnnotation.latitude);
    myAnnotation.longitude =[NSNumber numberWithFloat:[placesObj.venueDetailslongitude floatValue]];
    NSLog(@"myAnnotation.longitude is ----%@",myAnnotation.longitude);
    [annotaionsArray11 addObject:myAnnotation];
    NSLog(@"annotaionsArray11---%d",[annotaionsArray11 count]);

    [myAnnotation setButtonTag:i];
}

NSLog(@"annotaionsArray count after adding all locations..%d",[annotaionsArray11 count]);
 [mapView addAnnotations:annotaionsArray11];  }

Thanks in Advance for any help @harikrishna

1
By "correct direction" do you mean you want the pins to always be pointing northwards, even if the map is rotated so that west is at the top of the screen? If so, I'd council against it. I haven't seen that done in any other map app and your users will probably think it very strange that the pins have all fallen over.Craig
Are u sure you are getting the right responses from the web services?nhisyam

1 Answers

0
votes

Since this problem is somehow specific and not all the codes are shown here, I can only try to deduce from current available information. You want to show one of four different groups of parking locations according to user's heading, but when changing the heading, annotations are not changed.

Based on the assumption that web service is returning the right result, the problem may be caused by the way you use

MKAnnotationView * view = [mapView viewForAnnotation:obj];

Please check if you are using reuseIdentifier in your code.