I want to add multiple info windows to appear at the same time with out tapping on the marker .
How to implement this ?
Here is my code to add multiple markers and adding a infoWindow?
for (int i = 0 ; i < jsonDataDict.count; i ++) {
NSDictionary *newDict = [jsonDataDict objectAtIndex:i ];
double latitude = [[newDict valueForKey:#"lat"]doubleValue];
double longitute = [[newDict valueForKey:#"lng"]doubleValue];
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude, longitute);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
nameOfPlace = [newDict valueForKey:#"name"];
// distanceOfPlace = [newDict valueForKey:#"distance"];
marker.icon = [self image:[UIImage imageNamed:#"pinPopoye.png"] scaledToSize:CGSizeMake(75.0f, 60.0f)];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.infoWindowAnchor = CGPointMake(1.1, 0.70);
marker.map = self.mapView;
[self.mapView setSelectedMarker:marker];
//[self mapView:self.mapView markerInfoWindow:marker];
}
}
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 25)];
customView.backgroundColor = [UIColor colorWithRed:71.0/255.0 green:65.0/255.0 blue:65.0/255.0 alpha:0.8];
customView.layer.cornerRadius = 5;
customView.layer.masksToBounds = YES;
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 10)];
nameLabel.text = nameOfPlace;
nameLabel.textColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.font = [UIFont systemFontOfSize:8.0];
[customView addSubview:nameLabel];
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, 60, 10)];
distanceLabel.text = distanceOfPlace;
distanceLabel.textColor = [UIColor whiteColor];
distanceLabel.textAlignment = NSTextAlignmentCenter;
distanceLabel.font = [UIFont systemFontOfSize:8.0];
[customView addSubview:distanceLabel];
return customView;
}
Related
I have created Custom annotation by using this code.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *const AnnotatioViewReuseID = #"AnnotatioViewReuseID";
// MKAnnotationView
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotatioViewReuseID];
if (!annotationView) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotatioViewReuseID];
}
if ([annotation isKindOfClass:[FBAnnotationCluster class]]) {
FBAnnotationCluster *cluster = (FBAnnotationCluster *)annotation;
cluster.title = [NSString stringWithFormat:#"%lu", (unsigned long)cluster.annotations.count];
UIView *view = [[UIView alloc]init];
view.backgroundColor = [UIColor colorWithRed:33.0/255.0 green:191.0/255.0 blue:133.0/255.0 alpha:1.0];
UILabel *label = [[UILabel alloc]init];
label.text = cluster.title;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
UIFont *font = [UIFont fontWithName:#"Avenir-Medium" size:14.0];
label.font = font;
label.frame = CGRectMake(0, 0, [self widthOfString:label.text]+20, [self widthOfString:label.text]+20);
view.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.width) ;
view.layer.cornerRadius = view.frame.size.height/2;
view.layer.borderColor = [UIColor whiteColor].CGColor;
view.layer.borderWidth = 2.0;
view.clipsToBounds = true;
[view addSubview:label];
for (UIView *view in [annotationView subviews])
{
[view removeFromSuperview];
}
[annotationView addSubview:view];
annotationView.enabled = YES;
annotationView.annotation = annotation;
annotationView.canShowCallout = YES;
annotationView.pinTintColor = [UIColor clearColor];
} else {
annotationView.pinTintColor = [UIColor clearColor];
annotationView.layer.borderColor = [UIColor clearColor].CGColor;
annotationView.layer.borderWidth = 0.0;
FBAnnotation *a = (FBAnnotation*)annotation;
NSLog(#"amount is %f",a.amount);
UIImage *image = [UIImage imageNamed:#"icon-marker-select"];
UIImageView *imgView = [[UIImageView alloc]init];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumFractionDigits = 2;
NSString *result = [formatter stringFromNumber:[NSNumber numberWithDouble:a.amount]];
NSString *strData = [NSString stringWithFormat:#"%#%#",a.currency,result];
UIFont *font = [UIFont fontWithName:#"Avenir-Medium" size:14.0];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle,
NSForegroundColorAttributeName : [UIColor whiteColor]
};
CGSize textSize = [strData sizeWithAttributes:attributes];
CGRect textRect = CGRectMake(5, (image.size.height-textSize.height)/2 - 2, textSize.width , textSize.height);
UILabel *textLable = [[UILabel alloc]initWithFrame:textRect];
textLable.textColor = [UIColor whiteColor];
textLable.font = font;
textLable.text = strData;
UIImage *lightSymImg = [UIImage imageNamed:#"icon_lightning"];
UIImageView *lightImage = [[UIImageView alloc]init];
lightImage.image = lightSymImg;
CGRect imgRect = CGRectMake(textRect.origin.x+textRect.size.width, (image.size.height-lightSymImg.size.height)/2 - 2, lightSymImg.size.width,lightSymImg.size.height);
lightImage.frame = imgRect;
imgView.frame = CGRectMake(0, 0, imgRect.size.width + imgRect.origin.x+5, image.size.height);
imgView.image = image;
for (UIView *view in [annotationView subviews])
{
[view removeFromSuperview];
}
annotationView.annotation = annotation;
[annotationView addSubview:imgView];
[annotationView addSubview:textLable];
[annotationView addSubview:lightImage];
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
}
return annotationView;
}
And in didSelect method i have written this code for Present a view Controller
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog(#"didSelectAnnotationView");
if ([view.annotation isKindOfClass:[FBAnnotationCluster class]]) {
NSLog(#"FBAnnotationCluster select annotation");
}
else{
SpotDetailVC *locDetailVC = [[UIStoryboard storyboardWithName:#"SpotDetail" bundle:nil] instantiateViewControllerWithIdentifier:#"SpotDetailVC"];
[self presentViewController:navController animated:YES completion:nil];
}
}
Now When i click on annotation for the first Time it works but after dismiss that view again click on Same annotation it does not works.
Please help, i know it's Silly mistake but i am not able to configure out that one.
I am doing one function, when users pressed a button, map will indicate users' current location by on pulse animation annotation.
SKCoordinateRegion region;
region.center = CLLocationCoordinate2DMake(_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude);
region.zoomLevel = 14;
self.mapView.visibleRegion = region;
SKAnnotation *CurrentLocationAnnotation = [SKAnnotation annotation];
CurrentLocationAnnotation.identifier = 1;
//CurrentLocationAnnotation.annotationType = SKAnnotationTypeMarker;
CurrentLocationAnnotation.location = CLLocationCoordinate2DMake(_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude);
SKAnimationSettings *CurrentLocationanimationSettings = [SKAnimationSettings animationSettings];
CurrentLocationanimationSettings.animationType = SKPulseAnimation;
CurrentLocationanimationSettings.animationEasingType = SKAnimationEaseLinear;
CurrentLocationanimationSettings.duration = 7000;
[self.mapView addAnnotation:CurrentLocationAnnotation withAnimationSettings:CurrentLocationanimationSettings];
I tried it on simulator and iphone as well, both of them don't work at all.
How can i fix it? thanks in advance
CLLocationCoordinate2D cordinate;
- (void)viewDidLoad {
[super viewDidLoad];
self.mapview.delegate=self;
self.locationManager.delegate = self
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
[self.mapview setMapType:MKMapTypeSatellite];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 54, 122, 21)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.3];
label.text = #"title";
//create our view for the image
UIImageView *coloredView1 = [[UIImageView alloc]initWithFrame:CGRectMake(7, 5.5, 122, 75)];
coloredView1.image = [UIImage imageNamed:#"demo.png"];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(7, 5.5, 122, 75)];
view1.backgroundColor = [UIColor clearColor];
[view1 addSubview:label];
[view1 addSubview:coloredView1];
//create our view for the background image
UIImageView *coloredView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0.0, 136, 110)];
coloredView2.image = [UIImage imageNamed:#"mapMarker"];
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(7, 5.5, 122, 75)];
view1.backgroundColor = [UIColor clearColor];
[view2 addSubview:coloredView2];
[view2 addSubview:view1];
//create our view for the background image
UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 0.0, 136, 200)];
view3.backgroundColor = [UIColor clearColor];
[view3 addSubview:view2];
SKAnnotationView *annotationView = [[SKAnnotationView alloc] initWithView:customAnnotationView reuseIdentifier:#"reusableIdentifier"];
SKAnnotation *annotation = [SKAnnotation annotation];
annotation.identifier = 123456;
annotation.location = region.center;
annotation.annotationView = annotationView;
[self.mapView addAnnotation:annotation withAnimationSettings:[SKAnimationSettings animationSettings]];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
if([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *identifier = #"myAnnotation";
DraggableAnnotationView * annotationView = (DraggableAnnotationView *)[self.mapview dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[DraggableAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.image = [UIImage imageNamed:#"ic_map_pin.png"];
}
else
{
annotationView.annotation = annotation;
}
annotationView.delegate = self;
return annotationView;
}
if this code helpful for you then give your vote ... thank you
I apologize for asking this question for the millionth time, I've read the other questions asking the same thing and I couldn't find the answer to my problem!
I made a UITapGestureRecognizer that is just not working, can someone take a look at my code and tell me what I'm doing wrong?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(#"%#", self.tap);
}
Here is the code I used to add the cell to the screen.
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
You have missed adding self.cell to view. You are able to see labels with texts because you have added self.cell.clipsToBounds = NO;
All you have to do is add cell to view.
[self.view addSubview:self.cell]
PLEASE ADD
self.tap.delegate = self;
I have implemented infoWindow to show data of my multiple markers. But all i can display is same data in all by info windows .
How to show data which is related to that marker, so that there should be no repetition ?
Here is my code :
for (int i = 0 ; i < jsonDataDict.count; i ++) {
NSDictionary *newDict = [jsonDataDict objectAtIndex:i ];
double latitude = [[newDict valueForKey:#"lat"]doubleValue];
double longitute = [[newDict valueForKey:#"lng"]doubleValue];
nameStr = [newDict valueForKey:#"name"];
countJson = i ;
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitude, longitute);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = [newDict valueForKey:#"name"];
// marker.icon = [UIImage imageNamed:#"pin11.png"];
marker.icon = [self image:[UIImage imageNamed:#"pinPopoye.png"] scaledToSize:CGSizeMake(75.0f, 60.0f)];
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.infoWindowAnchor = CGPointMake(1.1, 0.70);
marker.map = self.mapView;
[self mapView:self.mapView markerInfoWindow:marker];
}
}
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
{ UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 25)];
customView.backgroundColor = [UIColor colorWithRed:71.0/255.0 green:65.0/255.0 blue:65.0/255.0 alpha:0.8];
customView.layer.cornerRadius = 5;
customView.layer.masksToBounds = YES;
// Orange Color ==== [UIColor colorWithRed:254.0/255.0 green:165.0/255.0 blue:4.0/255.0 alpha:0.5];
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 10)];
nameLabel.text = nameStr;
nameLabel.textColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.font = [UIFont systemFontOfSize:8.0];
[customView addSubview:nameLabel];
return customView;
}
Replace this statement:
nameLabel.text = nameStr;
with:
nameLabel.text = marker.title;
The problem is your using a shared NSString, nameStr, which gets overwritten at each iteration of your for loop. So, all label share the same string value when they are finally displayed. You could also do:
nameLabel.text = [nameStr copy];
and it should work -- but I think that using nameStr in your code was just a remnant of some previous "hack".
I am using Google IOS SDK and have implemented
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
to implement a custom view. When I show this view, it does not add a callout as Google map view normally does for its own markers. Can anyone please let me know how it would be possible to add this callout like pointer pointing towards the marker for this custom makerInfoWindow ?
Here is how to do it.
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
int popupWidth = 300;
int contentWidth = 280;
int contentHeight = 140;
int contentPad = 10;
int popupHeight = 200;
int popupBottomPadding = 16;
int popupContentHeight = contentHeight - popupBottomPadding;
int buttonHeight = 30;
int anchorSize = 20;
CLLocationCoordinate2D anchor = marker.position;
CGPoint point = [mapView.projection pointForCoordinate:anchor];
UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupHeight)];
float offSet = anchorSize * M_SQRT2;
CGAffineTransform rotateBy45Degrees = CGAffineTransformMakeRotation(M_PI_4); //rotate by 45 degrees
UIView *callOut = [[UIView alloc] initWithFrame:CGRectMake((popupWidth - offSet)/2.0, popupHeight - offSet, anchorSize, anchorSize)];
callOut.transform = rotateBy45Degrees;
callOut.backgroundColor = [UIColor blackColor];
[outerView addSubview:callOut];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, 190)];
[view setBackgroundColor:[UIColor whiteColor]];
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = 2.0f;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 0, contentWidth, 22)];
[titleLabel setFont:[UIFont systemFontOfSize:17.0]];
titleLabel.text = [marker title];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 24, contentWidth, 80)];
[descriptionLabel setFont:[UIFont systemFontOfSize:12.0]];
descriptionLabel.numberOfLines = 5;
descriptionLabel.text = [marker snippet];
[view addSubview:titleLabel];
[view addSubview:descriptionLabel];
[outerView addSubview:view];
return outerView;
}
This method is a GMSMapViewDelegate method, did you set the delegate to the view controller?
mapView.delegate = self;