1

I want to show multiple number of markers on goolemaps which is of different colours say red, green ,orange ,black marker shown in the below image.I have to show the price in the marker when I select some marker then Marker image must change same like the below imageI have to show the price in the marker when I select some marker then Marker image must change same like the below image

I am getting lat and long of marker from web services and using that data I am add markers,text in the marker using the below code but the Image is becoming blur .

-(void)hourelywebsevice:(NSString *)withlogintoken withlat:(double )weblat withlong:(double )weblong withcoustamerid:(NSString *)coustamerid{

NSLog(@"%@",withlogintoken);
NSLog(@"%f",weblat);
NSString *weblatstring=[NSString stringWithFormat:@"%f",weblat];
NSString *weblongstring=[NSString stringWithFormat:@"%f",weblong];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:weblatstring,@"lat",weblongstring,@"lon",coustamerid,@"customer_id",hourlyWebFromDateStr,@"fromtime",hourlyWebToDateStr,@"totime",withlogintoken,@"login_token",filtervechtypestr,@"vehicle_type",filteraminitesarray,@"amenities",filtertypearray,@"type", nil];
NSLog(@"%@",dict);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
[manager POST:@"http://park24x7.com/betav2/apipoint/gethourlyserviceproviders" parameters:dict progress:nil success:^(NSURLSessionTask *task, id responseObject) {

    NSLog(@"%@", responseObject);
    userarray=[responseObject objectForKey:@"data"];


    statusstring=[NSString stringWithFormat:@"%@", [responseObject objectForKey:@"status"]];

    NSInteger number=1;
    NSInteger statustwonumber=2;
    integerAsString = [NSString stringWithFormat:@"%ld",(long)number];
    useridstring1=[NSString stringWithFormat:@"%@", [userarray valueForKey:@"id"]];
    NSString *satustwostring= [NSString stringWithFormat:@"%ld",(long)number];
    alertstring =[responseObject objectForKey:@"message"];




    if([statusstring isEqualToString:integerAsString])
    {


        NSLog(@"%@",alertstring);
        //
        if (!(alertstring.length==0 )) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:alertstring
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [activityIndicatorView stopAnimating];
            [alert show];

        }else{

            vehicle_id=[NSString stringWithFormat:@"%@", [responseObject objectForKey:@"vehicle_id"]];
            Carnumber_string=[NSString stringWithFormat:@"%@", [responseObject objectForKey:@"vehicle_number"]];
                          int i;
            for (i=0; i<userarray.count; i++)
            {

                marker1 = [[GMSMarker alloc] init];
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,60,60)];
                NSString *space_remaingcountstring=[[userarray objectAtIndex:i ]valueForKey:@"space_remain"];
                NSInteger space_remaingcountInt=[space_remaingcountstring integerValue];
                UIImageView *pinImageView;
                int spacesAvilable,spacesOccupied,spacesRemain;
                NSString *spacesAvilableStr,*spacesOccupiedStr,*spacesRemainStr;


                spacesAvilableStr=[[userarray objectAtIndex:i ]valueForKey:@"space_available"];
                ;

                spacesOccupiedStr=[[userarray objectAtIndex:i ]valueForKey:@"space_occupied"];
                spacesRemainStr=[[userarray objectAtIndex:i ]valueForKey:@"space_remain"];

                spacesAvilable=[spacesAvilableStr intValue];
                spacesOccupied=[spacesOccupiedStr intValue];
                spacesRemain=[spacesRemainStr intValue];
                float precentage = 100*spacesOccupied/spacesAvilable;
                NSLog(@" precentage %f",precentage);

                if (spacesAvilable<=2) {
                    pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marker-orange-48"]];
                }
                else
                {
                    if (precentage<=50) {
                        pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marker-green-48"]];

                    }else if (precentage>50)
                    {
                        pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"marker-black-48"]];

                    }

                }


                UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(12, 7, 30, 20)];

                NSString *pricestring=[[userarray objectAtIndex:i ]valueForKey:@"hourly_price"];
                label.text=pricestring;
                label.textColor=[UIColor colorWithRed:187/255.0 green:0/255.0 blue:29/255.0 alpha:1];
                label.font=[UIFont systemFontOfSize:9.0f];
                [view addSubview:pinImageView];
                [pinImageView addSubview:label];
                UIImage *markerIcon = [self imageFromView:view];
                marker1.icon = markerIcon;
                NSString *resultdictlatstring=[[userarray objectAtIndex:i ]valueForKey:@"loc_lat"];
                float resultdictlatfloat=[resultdictlatstring floatValue];
                NSLog(@"%f",resultdictlatfloat);
                NSString *resultdictlongstring=[[userarray objectAtIndex:i ]valueForKey:@"loc_long"];
                float resultdictlongfloat=[resultdictlongstring floatValue];
                NSLog(@"%f",resultdictlongfloat);
                marker1.position = CLLocationCoordinate2DMake(resultdictlatfloat, resultdictlongfloat);
                NSLog(@"%f",marker1.position);
                marker1.appearAnimation = kGMSMarkerAnimationPop;
                marker1.map = hrlymapView;
                marker1.zIndex=i;

                NSLog(@"%d",marker1.zIndex);

            }
            [activityView stopAnimating];


        }

    }
    else if ([statusstring isEqualToString:satustwostring]){


        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:alertstring
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [activityIndicatorView stopAnimating];
        [alert show];
        [activityView stopAnimating];


    }
    else{


        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:alertstring
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [activityIndicatorView stopAnimating];
        [alert show];
        [hrlymapView clear];
        [activityView stopAnimating];
    }



}
      failure:^(NSURLSessionTask* operation, NSError* error) {

          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!!"
                                                          message:@"The Internet connection appears to be offline"
                                                         delegate:self
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
          [activityIndicatorView stopAnimating];
          [alert show];
          [activityView stopAnimating];

          [hrlymapView clear];
          NSLog(@"Error: %@", error);
      }];


 }
  - (UIImage *)imageFromView:(UIView *) view
{

UIGraphicsBeginImageContext(view.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
//    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
  //        UIGraphicsBeginImageContextWithOptions(view.frame.size, NO,            [[UIScreen mainScreen] scale]);
  //    } else {
  //        UIGraphicsBeginImageContext(view.frame.size);
  //    }
  //    [view.layer renderInContext: UIGraphicsGetCurrentContext()];
  //    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  //    UIGraphicsEndImageContext();
 //    return image;
 }

In didtap marker I am add belowcode to get the data from the marker I change the image of marker but I it not working

  - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)tappedMarker
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
_hourlyparkingSpotDetailsView.hidden=NO;
_hourlyTapOnMarkerLblView.hidden=YES;
 _monthlyParkingSpotDetailsView.hidden=NO;
_currentLocationBtn.frame=CGRectMake(_currentLocationBtn.frame.origin.x, _hourlyparkingSpotDetailsView.frame.origin.y-80, _currentLocationBtn.frame.size.width, _currentLocationBtn.frame.size.height);
//marker.map=nil;
NSLog(@"You tapped at %f,%f", tappedMarker.position.latitude,tappedMarker.position.longitude);
destinationlat_fvalue=tappedMarker.position.latitude;
destinationlon_fvalue=tappedMarker.position.longitude;
tappedMarker.icon=[UIImage imageNamed:@"pin-selected"];
NSLog(@"%d",tappedMarker.zIndex);


    _hourlyparkingSpotDetailsView.hidden=NO;
    _hourlyTapOnMarkerLblView.hidden=YES;
    _monthlyParkingSpotDetailsView.hidden=YES;
    _hourlyparkingSpotAddressLbl.text=[[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"address"];

    _hourlyParkingSpotPriceLbl.text=[NSString stringWithFormat:@"%@/h",[[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"hourly_price"]];
    _markerSpaceIdStr=[[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"space_id"];
    _hourlyParkingSpotZoneId=[[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"zone_id"];
    _hourlyParkingSptVehicleType=[[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"vehicle_type"];
    _hourlyreviews=[NSString stringWithFormat:@"%@ Reviews", [[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"reviws"]] ;
    _hourlyspacetype=[[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"space_type"];
    NSString *currency_symbol=[NSString stringWithFormat:@"%@", [[userarray objectAtIndex:tappedMarker.zIndex ]valueForKey:@"currency_symbol"]];
    _hourlyRupeeSymbolLbl.text=currency_symbol;
    [defaults setObject:currency_symbol forKey:@"currency_symbol"];

    _hourlyAutomatedLbl.text=_hourlyspacetype;
    _hourlyReviewsLbl.text=_hourlyreviews;



if(isMarkerActive == TRUE){
    [self unhighlightMarker:marker1];
}

 isMarkerActive = TRUE;
selectedMarker = tappedMarker;
 [self highlightMarker:tappedMarker];


return YES;

}
-(void)highlightMarker:(GMSMarker *)tappedMarker{
if([hrlymapView.selectedMarker isEqual:tappedMarker]){
    tappedMarker.icon = [UIImage imageNamed:@"pin-selected"];
}
 }
 -(void)unhighlightMarker:(GMSMarker* )UntappedMarker{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,70,70)];
UIImageView *pinImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin-red-2"]];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(23, 15, 29, 23)];

NSString *pricestring=[[userarray objectAtIndex:UntappedMarker.zIndex ]valueForKey:@"hourly_price"];
label.text=pricestring;
label.textColor=[UIColor colorWithRed:187/255.0 green:0/255.0 blue:29/255.0 alpha:1];
label.font=[UIFont systemFontOfSize:11.0f];
[view addSubview:pinImageView];
[pinImageView addSubview:label];
UIImage *markerIcon = [self imageFromView:view];
UntappedMarker.icon = markerIcon;

}

Thanks for quick answer,If you have any doubts in the code please let me know.

Nazir
  • 1,945
  • 24
  • 27
  • While making marker object, you can give an id to each marker. that later can be user for identifying the same. I thing maker has a userInfo dictionary that can be used for this purpose. – Pawan Rai Mar 14 '17 at 11:21
  • What is the question? What you mean under question:"How to add Multiple custom marker on google maps and how to change custom marker when it taped and untaped"? I see 3 different questions: 1. How to add multiple markers 2. How to make custom marker image 3. How to change maker icon on select and unselect – Nazir Mar 14 '17 at 11:28
  • In which part of your question 1, 2, 3 you face problems? – Nazir Mar 14 '17 at 11:29
  • hi Pawan Thanks for quick answer,How to do that – Satheeshkumar Naidu Mar 14 '17 at 11:31
  • @Nazir Thanks for quick response,I am facing problem in marker creation and Taping and untaping marker. – Satheeshkumar Naidu Mar 14 '17 at 11:33

2 Answers2

0

This is how add marker to map:

    // set `SOME_NAME` on your logic
    UIImage * markerIconImage = [UIImage imageNamed:SOME_NAME];

    //lat - CGFloat value
    //lng - CGFloat value
    CLLocation * location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];

    GMSMarker * marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
    marker.icon = markerIconImage;
    marker.map = mapView; // mapView - is your map view

To not duplicate the answers, here is how to manipulate on marker select and deselect actions: [Change Google Maps' Selected Marker or change marker's color? [iOS] - the logic will be same.

Community
  • 1
  • 1
Nazir
  • 1,945
  • 24
  • 27
  • I to used same code but the problem is the marker is not changing instead of that it is overrighting with changed marker,In other car If scrol the mapview the marker is changing but the postion is changing – Satheeshkumar Naidu Mar 14 '17 at 11:53
-1

While making marker object, you can give an id to each marker. that later can be used for identifying the same. I thing maker has a userInfo dictionary that can be used for this purpose

Example

GMSMarker *marker = [[GMSMarker alloc] init];
marker.userData = @{@"marker_id":[NSNumber numberWithInt:1]};

In delegate methods, you can get the marker id from userData.

 NSNumber *number = [marker.userData objectForKey:@"marker_id"];
Pawan Rai
  • 3,434
  • 4
  • 32
  • 42
  • Hi pawan,I am taking Index value using that index I am getting data I Tap on marker,My problem I have to change that marker Image and If and unselect that marker i should come to normal image, – Satheeshkumar Naidu Mar 14 '17 at 11:47
  • @SatheeshkumarNaidu You can have a another key "selected" (a bool value). when you select a maker set it value to YES & reload all annotations. [help refresh mapview](http://stackoverflow.com/questions/14131345/ios-refresh-annotations-on-mapview) – Pawan Rai Mar 14 '17 at 11:54
  • But the problem we is should not reload annotations. – Satheeshkumar Naidu Mar 14 '17 at 11:56
  • let me understand. you want to change the image of marker on tap right? on tap of other marker the first marker should change or will remain selected? – Pawan Rai Mar 14 '17 at 12:06
  • when you tap on first marker the image of first marker image should change ,when you click on the second marker then second marker image should change and first marker should be unselected. – Satheeshkumar Naidu Mar 14 '17 at 12:12
  • @SatheeshkumarNaidu following post has an excellent solution for your problem, I would suggest to follow it. [change-google-maps-selected-marker-or-change-markers-color-ios](http://stackoverflow.com/questions/21921375/change-google-maps-selected-marker-or-change-markers-color-ios) – Pawan Rai Mar 14 '17 at 12:32