m stucked in figuring out how can i set the viewport for my google map markers.
is there any way to do it in objC??
or i have to do it in my map itself??? if yes, then how???
My code for google map just shows the maps along with the markers,but when i click it,it just shows the name of the place in a small box.. :(
how to make it a proper viewport???
here's the viewport image:
1) You can subclass MKAnnotationView. I think it's the best way to solve your problem.
2) You can customize your annotation callout view, e.g:
- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString* ItemAnnotationIdentifier = #"itemAnnotationIdentifier";
MKPinAnnotationView* pinView = (MKPinAnnotationView *)
[theMapView dequeueReusableAnnotationViewWithIdentifier:ItemAnnotationIdentifier];
if (!pinView)
{
// if an existing pin view was not available, create one
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation
reuseIdentifier:ItemAnnotationIdentifier]
autorelease];
customPinView.canShowCallout = YES;
UIImage *logo = [ImageProcessor scaleImage:[UIImage imageNamed:#"logo.png"] toSize:CGSizeMake(30, 30)];
customPinView.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:logo] autorelease];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
rightButton.tag = [((ItemAnnotation *)annotation).itemId intValue];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
}
else
{
pinView.annotation = annotation;
}
return pinView;
}
Related
I'm a noob to iphone development and i'm trying to add a custom button to an annotation callout. I have no problems adding a regular button to rightCalloutAccessoryView, but it's just not working for custom style. My image size is 32 x 32. I also want to add a custom map pin following this stackoverflow question here Any help is greatly appreciated.
MY CODE
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
// Define your reuse identifier.
static NSString *identifier = #"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.animatesDrop = YES;
annotationView.image = [UIImage imageNamed:#"myimage.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setImage:[UIImage imageNamed:#"phony2.png"] forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;
return annotationView;
}
return nil;
}
[rightButton addTarget:self
action:#selector(showDetails:)
Remove this line
and set frame of the button
[rightButton setFrame:CGRectMake(0,0,32,32)];
and get the tap action from the degate method
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control;
where you get the annotation view.
Hope this will help you.
I want to add a UIImage in MKMapview.
i.e.: One a particular location, I want to add an image called "apple.jpg".
I don't know how to do that.
As I have already added a draggable pin as an annotation. But I don't know if I can add multiple images or not.
For add image as a Part of MKMapView see this Tutorial...
i.ndigo mkmapview
and for add Image as a Pin on MapView then use this bellow code...
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] init];
annotationView.image = [UIImage imageNamed:#"apple.png"];
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
annotationView.rightCalloutAccessoryView.tag = 101;
annotationView.canShowCallout = YES;
[yourMapView addAnnotation:annotationView];
as #Kassem Bagher mentions, you need to create custom MKAnnotationView , check whatever tutorial (example)
Your image must be in png format see the code below.
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinView"];
if (!pinView) {
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinView"] autorelease];
pinView.image = [UIImage imageNamed:#"apple.png"];
pinView.canShowCallout = YES;
}
}
This is working code and i already done with below code no necessory image is .png , .jpg or .gif its any formate you can use it
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinAnnotation = nil;
NSString *defaultPinID = #"myPin";
pinAnnotation = (MKPinAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinAnnotation == nil )
pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinAnnotation.image = [UIImage imageNamed:#"marker_postoffice.png"];
pinAnnotation.annotation = annotation;
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
// now you can set diff image by [annotation title] you can set diff image with if else condition like below code
if([[annotation title] isEqualToString:objAppDelegate.OfficePinTitle])
{
pinAnnotation.image = [UIImage imageNamed:#"marker_postoffice.png"];
pinAnnotation.annotation = annotation;
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
infoButton addTarget:self
action:#selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
else
{
pinAnnotation.image = [UIImage imageNamed:#"marker_red_postoffice.png"];
pinAnnotation.canShowCallout = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[infoButton addTarget:self action:#selector(showDetails)forControlEvents:UIControlEventTouchUpInside];
pinAnnotation.rightCalloutAccessoryView = infoButton;
}
}
I have a table view with an ordered list of objects, ordered by distance between me and the object. I need to show annotations in a map to show to the user the position of the objects but i need to show in the same order than table´s order because then i need to get with one has been pressed to show info of each object.
The problem is that when I put the annotations in the map each time I put them the tag number of the annotation is different. Is like the map added the annotations in a random order.
I have an annotations array and then I add that array to the map:
MKPointAnnotation* tallerAnnotation = [[MKPointAnnotation alloc] init];
[tallerAnnotation setCoordinate:CLLocationCoordinate2DMake([taller getLatitudTaller], [taller getLongitudTaller])];
[tallerAnnotation setTitle:[taller getNombre]];
[tallerAnnotation setSubtitle:[taller getDomicilio]];
[annotations addObject:tallerAnnotation];
And then add the array to the map:
[mapa addAnnotations:annotations];
Here is the code where I custom the annotation:
- (MKAnnotationView *) mapView: (MKMapView *) mapa viewForAnnotation: (id <MKAnnotation>) annotation_ {
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapa dequeueReusableAnnotationViewWithIdentifier: #"YourPinId"];
if (pin == nil) {
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation_ reuseIdentifier: #"YourPinId"];
}
else {
pin.annotation = annotation_;
}
if ([[annotation_ title]isEqualToString:#"Tu posición"])
pin.pinColor = MKPinAnnotationColorRed;
else
{
[pin setCanShowCallout:YES];
pin.pinColor = MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTag:posicionActualTaller];
pin.rightCalloutAccessoryView = rightButton;
UILabel *l1=[[UILabel alloc] init];
l1.frame=CGRectMake(0, 5, 50, 15);
l1.backgroundColor = [UIColor clearColor];
l1.textColor = [UIColor whiteColor];
l1.font=[UIFont fontWithName:#"Arial Rounded MT Bold" size:(10.0)];
l1.text=[[[mainDelegate getTalleres]objectAtIndex: posicionActualTaller]getDistancia];
posicionActualTaller +=1;
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,15)];
[leftCAV addSubview : l1];
pin.leftCalloutAccessoryView = leftCAV;
}
pin.animatesDrop = YES;
return pin;
}
I would like to do something like this:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"Tag del taller = %i",[control tag]);
[mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:[control tag]]];
[self detalleTaller];
}
[control tag] has differents values each time I enter into map view so that way it´s impossible do something like [objects objectAtIndex:[control tag]];
Please I need help.
Thanks in advance.
Ok finally I have found a solution.
When I create the
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(getInfoTalleres:) forControlEvents:UIControlEventTouchUpInside];
for the annotation I assign the annotation title to the button this way:
[rightButton setTitle:[annotation_ subtitle] forState:UIControlStateNormal];
Then:
-(void)getInfoTalleres:(UIButton* )sender
{
for (int i = 0; i < [[mainDelegate getTalleres]count]; i++)
{
if ([[sender currentTitle] isEqualToString:[[[mainDelegate getTalleres]objectAtIndex:i]getDomicilio]])
{
[mainDelegate setTallerActual:[[mainDelegate getTalleres]objectAtIndex:i]];
[self detalleTaller];
break;
}
}
}
Hope somebody can use this solution.
Thanks a lot Cocoa.
Regards.
Check the Answer here it access title property Determine which MKPinAnnotationView was selected? and the tutorial http://www.scribd.com/doc/91629556/192/The-MKAnnotationView-class
let me know if you still not find answer
I have an custom annotation set up in my mapview using below code. Problem is that the position of the annotation image is incorrect, it put's it a bit to the right of the actual location instead of spot on like when using the regular Pins.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
return annotationView;
else
{
MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:AnnotationIdentifier] autorelease];
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:#"townhouse.png"]];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:#selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
annotationView.canShowCallout = YES;
annotationView.draggable = YES;
return annotationView;
}
return nil;
}
Any help at all would be grateful!
/Marcus
Marcus,
use the centerOffset property of MKAnnotationView to move your custom annotation view.
annotationView.centerOffset = CGPointMake(xOffset, yOffest);
I am developing a custom pins in a MapView in my iPhone app.
Here is the code:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *AnnotationViewID = #"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
if ([[annotation title] isEqualToString:NSLocalizedString(#"Current Location",#"")]) {
MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
//pin.pinColor = MKPinAnnotationColorRed;
annotationView = pin;
}
else
{
NSString *pin = [NSString stringWithFormat:#"pin%i.png",[[annotation imgNumber] intValue]];
annotationView.image = [UIImage imageNamed:pin];//Canviar la chincheta per numero
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[annotationView setRightCalloutAccessoryView:button];
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
/********* Imagen a la izquierda en la burbuja *********/
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
// Set up the Left callout
UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
myDetailButton.frame = CGRectMake(0, 0, 23, 23);
myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Set the image for the button
//[myDetailButton setImage:[UIImage imageNamed:#"botocorreu.png"] forState:UIControlStateNormal];
NSString *detailButton = [NSString stringWithFormat:#"%i",[[annotation imgNumber] intValue]];
[myDetailButton setImage:[UIImage imageNamed:detailButton] forState:UIControlStateNormal];
// Set the button as the callout view
annotationView.leftCalloutAccessoryView = myDetailButton;
annotationView.canShowCallout = YES;
}
return annotationView;
}
This works good but show me the users location with a red pin.
I check the "show user location" in the IB.
i would like to use the default blue point which will move it automatically when the user is moving, isn't correct?
How can i do that?
Thanks a lot.
Add this to the top of the viewForAnnotation method:
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
The special user location annotation is of type MKUserLocation and returning nil in that case tells the map view to draw the default view for it which is the blue dot.
You can also remove these lines since they will no longer be needed:
if ([[annotation title] isEqualToString:NSLocalizedString(#"Current Location",#"")]) {
MKPinAnnotationView *pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
//pin.pinColor = MKPinAnnotationColorRed;
annotationView = pin;
}
else