ios - Fetching Selected MapAnnotation Details from plist -
i'm trying fetch particular details on mapannotation
user clicks.the details fetched plist
file. function i've used - (void)mapview:(mkmapview *)mapview didselectannotationview:(mkannotationview *)view;
on using this.while click on particular annotation
fetches location details frommap
"pune, maharashtra, republic of india @ <+18.50565666,+73.85559082> +/- 0.00m"
and not checks plist values.even after given these values attributes in plist latlong each location.can solve problem?
my plist file details:
2013-02-18 12:16:56.039 mapview[9407:13d03] { contents = "this fish found in south asia"; imagesname = "kfish.jpg"; latlong = "bangalore, karnataka, republic of india @ <+12.98333330,+77.58333330> +/- 0.00m"; listname = kfish; location = "bangalore, karnataka, india"; } 2013-02-18 12:16:56.039 mapview[9407:13d03] { contents = "this fish found in south asia"; imagesname = "wallfish.png"; latlong = "delhi, republic of india @ <+28.59100531,+77.08826373> +/- 0.00m"; listname = wallfish; location = "delhi, india"; } 2013-02-18 12:16:56.039 mapview[9407:13d03] { contents = "this fish found in south asia"; imagesname = "fish.jpg"; latlong = "chennai, tamil nadu, republic of india @ <+13.07983465,+80.24625757> +/- 0.00m"; listname = fish; location = "chennai,tamilnadu,india"; } 2013-02-18 12:16:56.039 mapview[9407:13d03] { contents = "this fish found in south asia"; imagesname = "bluefish.jpg"; latlong = "mumbai, maharashtra, republic of india @ <+18.99460885,+72.82287598> +/- 0.00m"; listname = bluefish; location = "mumbai, maharashtra, india"; } 2013-02-18 12:16:56.040 mapview[9407:13d03] { contents = "this fish found in south asia"; imagesname = "rarefish.jpg"; latlong = "chennai, tamil nadu, republic of india @ <+13.07983465,+80.24625757> +/- 0.00m"; listname = rarefish; location = "chennai,tamilnadu,india"; }
my coding:
- (void)geocoderequest { clgeocoder *geocoder = [[clgeocoder alloc]init]; (int = 0; < total; i++) { nsstring *address = [allvalues objectatindex:i] ; [geocoder geocodeaddressstring:address completionhandler:^(nsarray *placemarks, nserror *error) { clplacemark *geocodedplacemark = [placemarks objectatindex:0]; mkplacemark *placemark = [[mkplacemark alloc] initwithcoordinate:geocodedplacemark.location.coordinate addressdictionary:geocodedplacemark.addressdictionary]; int zoomlevel=20; mkcoordinateregion region=mkcoordinateregionmake(geocodedplacemark.location.coordinate, mkcoordinatespanmake(zoomlevel,zoomlevel)); [self.mapview addannotation:placemark]; [self.mapview setregion:region animated:yes]; [self.mapview setzoomenabled:yes]; }]; } -(void)mapview:(mkmapview *)mapview didselectannotationview:(mkannotationview *)view{ total = self.arrayimages.count; nsarray *newindex = [mapview selectedannotations]; nslog(@"sel :%@",newindex); (index = 0; index < total; index++) { dict = [self.arrayimages objectatindex:index]; nslog(@"%@",dict); if (latlong == newindex) { cgrect rec = cgrectmake(0, 0, 250, 250); [self setview:[[[uiview alloc] initwithframe:rec] autorelease]]; [[self view] setbackgroundcolor:[uicolor whitecolor]]; uiimage *img = [uiimage imagenamed:[dict objectforkey:@"imagesname"]]; uiimageview *im = [[uiimageview alloc] initwithimage:img]; uilabel *lab1 = [[uilabel alloc] init]; uitextview *detailsview = [[uitextview alloc] init]; lab1.text = [nsstring stringwithformat:[dict objectforkey:@"listname"]]; detailsview.text = [nsstring stringwithformat:[dict objectforkey:@"contents"]]; detailsview.editable = no; detailsview.bounds = cgrectmake(20, 20, 150, 150); detailsview.frame = cgrectmake(210, 720, 390, 350); detailsview.font = [uifont systemfontofsize:24]; lab1.bounds = cgrectmake(20, 20, 150, 150); lab1.frame = cgrectmake(360, 600, 90, 50); lab1.font = [uifont systemfontofsize:24]; im.bounds = cgrectmake(10, 10, 50, 50); im.frame = cgrectmake(200, 120, 370, 450); [uiview transitionwithview:[self view] duration:1.5 options:uiviewanimationoptiontransitioncurlup animations:^ { [[self view] addsubview:im]; [[self view] addsubview:lab1]; [[self view] addsubview:detailsview]; }completion:nil]; } } }
mapview works fine.but on fetching selected location it's not working.can help me have went wrong??
thanks in advance.
in didselectannotationview
method, appear iterating through arrayimages
, looking 1 latlong == newindex
. couple of thoughts:
using equality operator, ==
comparing objects, not prudent technique. have no assurances map view's annotation same object (if properties using copy
, not equal, because address object , theirs may different). , if did happen work, it's not reliable way work.
i'm not quite if follow logic, anyway, if equality operator did work, because in geocoderequest
making new mkplacemark
, allvalues
, in didselectannotationview
you're comparing arrayimages
. , if
statement in didselectannotationview
looking @ latlong
, newindex
(the former don't see setting, latter array of annotations). doesn't create sense me. don't see how perchance match.
but 2 prior points irrelevant, because proper solution not add together mkplacemark
map , iterate through list in didselectannotationview
hunting match, instead, create own annotation class of properties want able retrieve when user taps on annotation view. or, @ least, have own annotation subclass , define property can utilize particular details array. either approach works. key not seek iterate through objects looking match, rather define custom annotation has need find right away.
for examples of custom annotations, see defining custom annotation object section of location awareness programming guide. 1 time you've defined own annotation subclass of properties think you'll need, (a) alter geocoderequest
create instances of new annotation subclass rather mkplacemark
; , (b) alter didselectannotationview
grab necessary info annotation
(which property of annotationview
). way, eliminates headache of iterating through list of possible annotations looking match.
fyi, in s.o. answer, give examples on options on handling callouts, popovers, etc., maybe there there might helpful background you, too. don't utilize custom annotations there, maybe gives ideas integration mapkit.
let me illustrate practice. let's want annotate our map, want maintain track of additional properties. first, i'll define annotation custom class has additional properties. (i'll subclass mkpointannotation
create life little easier me.)
@interface customannotation : mkpointannotation @property (nonatomic) nsuinteger index; @property (strong, nonatomic) nsstring *property1; @property (strong, nonatomic) nsstring *property2; @property (strong, nonatomic) nsstring *property3; @end
now, when want add together annotations map view, i'll utilize custom annotation class, making sure set whatever properties want. save numeric index in array, or save actual properties care about, or both:
customannotation *annotation = [[customannotation alloc] init]; // set mkpointannotation standard properties annotation.title = item.name; annotation.coordinate = item.placemark.coordinate; // set custom properties annotation.index = idx; annotation.property1 = ...; annotation.property2 = ...; annotation.property3 = ...; // add together annotation map view [annotations addobject:annotation];
or, if wanted take advantage of of properties of mkplacemark
, following:
@interface customannotation : mkplacemark @property (strong, nonatomic) nsstring *title; // mkplacemark doesn't have title, let's add together our own property @property (nonatomic) nsuinteger index; @property (strong, nonatomic) nsstring *property1; @property (strong, nonatomic) nsstring *property2; @property (strong, nonatomic) nsstring *property3; @end
and you'd create annotation you'd create mkplacemark
, again, set our custom properties:
customannotation *annotation = [[customannotation alloc] initwithplacemark:item.placemark]; // set custom properties annotation.title = item.name; annotation.index = idx; annotation.property1 = ...; [annotations addobject:annotation];
now, when select annotation view, can handle accordingly:
- (void)mapview:(mkmapview *)mapview didselectannotationview:(mkannotationview *)view { if ([view.annotation iskindofclass:[customannotation class]]) { customannotation *annotation = view.annotation; // note, don't have iterate through arrays figure out // annotation i'm dealing with; can @ custom properties nslog(@"index = %d; property1 = %@", annotation.index, annotation.property1); // whatever want annotation; // annotations's details using custom `index` property defined, // or if defined annotations store of detail properties // needed, can access them straight } }
as aside, rather using didselectannotationview
, might want adopt more standard mkmapview
behavior, , show standard callout bubble, set right callout bubble accessory, , handle clicking on callout accessory:
- (mkannotationview *)mapview:(mkmapview *)mapview viewforannotation:(id<mkannotation>)annotation { if (![annotation iskindofclass:[customannotation class]]) homecoming nil; mkannotationview *annotationview = [[mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:@"customannotationview"]; annotationview.canshowcallout = yes; annotationview.rightcalloutaccessoryview = [uibutton buttonwithtype:uibuttontypedetaildisclosure]; homecoming annotationview; } - (void)mapview:(mkmapview *)mapview annotationview:(mkannotationview *)view calloutaccessorycontroltapped:(uicontrol *)control { if (![view.annotation iskindofclass:[customannotation class]]) return; customannotation *annotation = view.annotation; // note, don't have iterate through arrays figure out // annotation i'm dealing with; can @ custom properties nslog(@"index = %d; property1 = %@", annotation.index, annotation.property1); // whatever want annotation; // annotations's details using custom `index` property defined, // or if defined annotations store of detail properties // needed, can access them straight }
ios objective-c mkmapview plist
No comments:
Post a Comment