I'm developing an app to show the stores available in a particular zipcode. I start with the current location. I'm getting a blue pulsating dot for my current location. when I enter a zipcode I'm able to show the annotations in the mapview. I have a button "current location" which can to go back to the current location again. However the second time I'm not getting the blue dot? How do I get it, this is the code:
- (void)showCurrentLocation
{
self.mapView.showsUserLocation = YES;
GPSAnnotation *annotation = [[GPSAnnotation alloc] initWithCoordinate:self.mapView.userLocation.coordinate];
annotation.title = #"Location";
[self.mapView addAnnotation:annotation];
[annotation release];
MKCoordinateRegion zoomIn;
zoomIn.center.latitude = self.mapView.userLocation.coordinate.latitude;
zoomIn.center.longitude=self.mapView.userLocation.coordinate.longitude;
zoomIn.span.latitudeDelta = .1f;
zoomIn.span.longitudeDelta =.1f;
self.mapView.showsUserLocation = YES;
[self.mapView setRegion:zoomIn animated:TRUE];
}
// annotation view
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(GPSAnnotation *)annotation
{
if ([[annotation title] isEqualToString:#"Location"])
{
MKAnnotationView *annotationView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil] autorelease];
annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:#"blueDot"];
annotationView.annotation = annotation;
return annotationView;
}
else if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
}
Do you make somewhere a [self.mapView removeAnnotations:self.mapView.annotations] to empty the map? Thats a common error because you also remove the userlocation by doing that.
Related
I have a MKMapView that is having a very strange problem which is kind of hard to explain.
I have an MKOverlay that is saved between sessions by saving the points of the overlay to a file and loading the overlay in viewDidLoad. So here's the problem:
Steps
I add an MKOverlay to the MKMapView
On the second or third run after adding the MKOverlay to the map
everything on the map gets overlayed by an overlay of the same color
as the one I added. Everything on the map with the exception of the
view of the map that you can see when the view loaded.
Other weird things:
This problem only occurs when mapView.showsUserLocation is set to
true
This problem only occurs on iOS5. Everything works fine on iOS6.
If you zoom out the colored overlay re sizes the visible portion of
the map to fill the map at the new level at which you zoomed out to.
This guy is having the same problem, although his solution did not work for me.
Trouble with overlay using MKPolyline & MKPolylineView
Here is some of my code:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil; //switch this to MKAnnotationView and use .image property if pins need to be images
if(annotation == self.startingPin || annotation == self.endingPin){
static NSString *defaultPinID = #"com.MagnusDevelopment.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pinView == nil)pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.canShowCallout = YES;
pinView.animatesDrop = TRUE;
if(annotation == self.startingPin){
//starting pin
pinView.pinColor = MKPinAnnotationColorGreen;
}else{
//ending pin
pinView.pinColor = MKPinAnnotationColorRed;
}
}
return pinView;
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *aView = [[MKPolylineView alloc] initWithPolyline:(MKPolyline *)overlay];
aView.lineWidth = 7.0;
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.6];
return aView;
}
Code called when the view loads, I have logged the coordinates that are being added to the polyline array right before they are added, and everything is fine and dandy!
NSInteger numberOfSteps = pointsArray.count;
NSLog(#"number of steps: %i",numberOfSteps);
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [pointsArray objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[self.mainMap addOverlay:polyLine];
Here is a picture of what happens when the overlay appears and the user pans to the side on the map:
Any ideas about what the problem is?
You're using dequeueReusableAnnotationViewWithIdentifier, but you're only assigning an annotation to pinView if the result of dequeueReusableAnnotationViewWithIdentifier was nil.
As that method will often return an existing MKPinAnnotationView instance, this should instead be:
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if (pin == nil) {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
} else {
pin.annotation = annotation;
}
This might be the problem: When the mapview tries to access the view for user's current location, you are passing your customized view.
Try adding this with your existing implementation.
- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
return yourView;
}
PS: This may not be an issue of overlay view.
Best Regards.
Following is my MapKitDisplayViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 22.569722 ;
region.center.longitude = 88.369722;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = [person name];
ann.subtitle = [person address];
ann.person = person;
ann.coordinate = region.center;
[mapView addAnnotation:ann];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
UIImageView *iconView = [[UIImageView alloc] initWithImage: [UIImage imageWithString:#?????????????????];
annotationView.leftCalloutAccessoryView = iconView;
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
return pinView;
}
You will see the line where it'll say ???????? I need to display an image there.
When the user clicks a pin point, then the popup will appear. Title, subtitle and the image is suppose to be displayed on the popup. I believe that the following line should display the image for that popup. But i am unable to pass the Person object's (or the pipoint which contains the person object) image URL to this method. Can you help me display the selected Image URL here.
It doesn't fail. I need to know how to pass the person object
selected by the user to viewForAnnotation method
Ahhh, I see know. I had a similar issue, and it can be a be tricky. I'm right in the middle of reworking my Annotation code, so you can see it since it doesn't work, but I you need to store something away that you can then later retrieve.
I made an object, MKAnnotation.
#interface FotoMapLocation : NSObject <MKAnnotation> {
So when I make an annotation, I create a FotoMapLocation object.
FotoMapLocation *annotation = [[FotoMapLocation alloc] initWithTitle:title
subtitle:subtitle
foto:aFoto
thumbnailImage:aFoto.thumbnailImage
];
[mapView addAnnotation:annotation];
Now, you can store anything here. So store your Person object in your subclass.
I know this is only halfway there, and very rambling. But I was right in the middle of ripping that code out, and then another fire came up, so it is currently in an in-between state. But I expect to return to it in a few days. I'll check back and if you don't have anything I'll update this answer.
This is what i did, in the viewForAnnotation method
DisplayMap *dm= (DisplayMap *)annotation;
NSString *url = [dm.person urlOfPerson];
I am new with MapKit and all its functionalities and therefore, stuck at trying to display a pin. I followed an online video tutorial on how to find user current location and drop a pin there. But when I typed the method for the pin, I do not get it at all. I would like to know where did I go wrong for this.
- (void)viewDidLoad{
[super viewDidLoad];
self.title = #"Location";
mapView.showsUserLocation = YES;
self.locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(#"Annotation view run");
MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"currentloc"];
pin.animatesDrop = YES;
return pin;
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
// once location is determined, center to that location.
location = newLocation.coordinate;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = 0.003;
span.longitudeDelta = 0.003;
region.span = span;
[mapView setRegion:region animated:FALSE];
}
Any advice of where I done wrong would be great!
As your method is never got invoked, you should do:
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated {
[aMapView removeAnnotation:[self point]];
mkShape.coordinate = aMapView.centerCoordinate;
[aMapView addAnnotation:mkShape];
}
And you should create your MKAnnotationView properly.
You need create two properties in .h file first (for performance reason):
MKPointAnnotation *mkShape;
MKAnnotationView *annotationView;
then use the code below to create your MKPointAnnotation
- (void)createShape
{
if (!mkShape) {
mkShape = [[MKPointAnnotation alloc] init];
mkShape.title = nil;
mkShape.subtitle = #"test description";
}
}
- (id <MKAnnotation>)point
{
[self createShape];
// Make sure to check if this is an MKPointAnnotation. MKOverlays also
// conform to MKAnnotation, so it isn't sufficient to just check to
// conformance to MKAnnotation.
if ([mkShape isKindOfClass:[MKPointAnnotation class]])
return (id <MKAnnotation>)mkShape;
return nil;
}
- (MKAnnotationView *)annotationView
{
if (!annotationView) {
id <MKAnnotation> annotation = [self point];
if (annotation) {
MKPinAnnotationView *pin =
[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
pin.canShowCallout = YES;
pin.animatesDrop = YES;
pin.draggable = NO;
annotationView = pin;
}
}
return annotationView;
}
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isEqual:mkShape]) {
return [self annotationView];
}
return nil;
}
At the end, don't forget to release your properties.
- (void)dealloc
{
[annotationView release];
[mkShape release];
[super dealloc];
}
I have two questions,
How to create an overlay on a MKMapkitView from user's touch down events? i.e. To keep it simple, the user touches down and it creates a MKCircle overlay
How does the Maps application implements the "dropped pin" on touch down? Anybody knows or have some code examples on how to accomplish something similar?
Any pointers would be greatly appreciated. I've been googling and reading lots of docs without much success as you can see.
Below is an example that creates a circle and drops a pin where the user touches and holds their finger for 1 second. It uses a UILongPressGestureRecognizer which is added to the mapView wherever the map is initialized (eg. viewDidLoad).
Make sure the mapView's delegate is set also.
// In viewDidLoad or where map is initialized...
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //user must hold for 1 second
[mapView addGestureRecognizer:lpgr];
[lpgr release];
...
- (void)handleLongPress:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate = [mapView convertPoint:touchPoint toCoordinateFromView:mapView];
//add pin where user touched down...
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = #"Hello";
[mapView addAnnotation:pa];
[pa release];
//add circle with 5km radius where user touched down...
MKCircle *circle = [MKCircle circleWithCenterCoordinate:touchMapCoordinate radius:5000];
[mapView addOverlay:circle];
}
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
{
MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
circleView.fillColor = [UIColor redColor];
return circleView;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationIdentifier = #"Annotation";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.animatesDrop = YES;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
i have got the Latitude and Longitude from XML Parsing how can i show MapAnnotation(pin) on the Map,Please help me....
Create a new class that implements the MKAnnotation protocol. This will hold the lat/long, and will also have a title and description which will be displayed if you select the annotation after it has been rendered on your map.
The controller for the view that will display the map will need to implement the - - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation; method of the MKMapViewDelegate protocol. The code in this method will look something like the code at the bottom (apologies for the poor formatting, I couldn't get right in here or at the bottom).
Then at some point in your controller code you will need to call something along the lines of [self.mapView addAnnotation: annotation]; where annotation is an instance of your custom annotation class created in step 1, with the lat/long set etc.
Finally, so that the viewForAnnotation method gets called correctly, and is something that is easy to miss, in interface builder, make sure that you set the delegate outlet of the MKMapView to be your controller (that implements the MKMapViewDelegate protocol.
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation {
static NSString *AnnotationViewIdentifier = #"annotationViewIdentifier";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier: AnnotationViewIdentifier];
if (annotationView == nil) {
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: AnnotationViewIdentifier] autorelease];
// This is all optional and depends on your requirements
annotationView.animatesDrop = NO;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.enabled = YES;
annotationView.pinColor = MKPinAnnotationColorGreen;
}
return annotationView;
}
First you want to create MKAnnotation Protocol class (Here MyAnnotation class implements the MKAnnotation Protocol).This class should be includes lat, long, title, subtitle and also whatever you want.
Second, In your view controller, where you want to display the pin and you will implement this code,
AnnObj = [[MyAnnotation alloc] init];
AnnObj.latitude = [[latitude objectAtIndex:storyIndex] floatValue];
AnnObj.longitude = [[longitude objectAtIndex:storyIndex] floatValue];
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
region.center.latitude = [[latitude objectAtIndex:storyIndex] floatValue];
region.center.longitude = [[longitude objectAtIndex:storyIndex] floatValue];
span.latitudeDelta = 0.0005;
span.longitudeDelta = 0.0005;
region.span = span;
[mapview setRegion:region animated:TRUE];
AnnObj.title = titleString;
AnnObj.subTitle = subTitleString;
NSString * titleString = [[buildingNames objectAtIndex:storyIndex] stringByReplacingOccurrencesOfString:#"\n\t" withString:#""];
[eventPoints addObject:AnnObj];
[mapview addAnnotations:eventPoints];
Third, Implement the MKAnnotation delegate method,
- (MKAnnotationView *)mapView: (MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>) annotation {
if (lmapView.userLocation == annotation){
return nil;
}
MKAnnotationView* myCusAnn = (MKAnnotationView*)[lmapView dequeueReusableAnnotationViewWithIdentifier:#"eventview"];
if(myCusAnn == nil)
{
myCusAnn = [[[ MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"eventview"] autorelease];
}
myCusAnn.canShowCallout = YES;
[myCusAnn setEnabled:YES];
return myCusAnn;
}
I hope it will help you.