How to set custom markers (image) on GMSMapView - ios

I tried this
GMSMarker *marker = [[GMSMarker alloc]init];
marker.icon = [UIImage imageNamed:#"pin2.png"];
marker.position = CLLocationCoordinate2DMake(lati, longi);
marker.groundAnchor = CGPointMake(0.5, 0.5);
marker.draggable = YES;
marker.map = mapView;
but I did not get the image.

I think you are missing this line:
[mapView setSelectedMarker:marker];

Related

Marker Title is not displaying on GoogleMapView in iOS Objective C

Hello all I am using GoogleMaps to display multiple markers on mapview. Maps is working fine and it display multiple markers according to coordinate. but it it not displaying the title of the location.
I am using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:cityLattitude
longitude:cityLongitude
zoom:7];
[self.mapView animateToCameraPosition:camera];
//GMSMapView *mapView; //= [GMSMapView mapWithFrame:CGRectZero camera:camera];
[mapView animateToCameraPosition:camera];
mapView.indoorEnabled = NO;
mapView.mapType = kGMSTypeNormal;
viewFormap = mapView;
mapView.accessibilityElementsHidden = NO;
mapView.myLocationEnabled = YES;
NSLog(#"User's location: %#", mapView.myLocation);
mapView.delegate = self;
GMSMarker * marker = [[GMSMarker alloc]init];
marker.position = CLLocationCoordinate2DMake(cityLattitude, cityLattitude );
marker.map = mapView;
[mapView animateToCameraPosition:camera];
}
To display annotations i used the following code:
-(void)plotMutliplePinsOnMap//:(NSArray *)MapDataArray
{
for(int i=0;i<[MapDataArray count];i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
// Set label properties as required including marker Name
UIImageView *markerIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(35, 10, 30, 50)];
// Set image properties as required including image which will be displayed as marker
UIView *markerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[markerView addSubview:label];
[markerView addSubview:markerIconImageView];
//GMSMapView *mapView = [[GMSMapView alloc]init];
viewFormap = mapView;
latitude = [[[MapDataArray objectAtIndex:i]valueForKey:#"latitude"] doubleValue];
longitude = [[[MapDataArray objectAtIndex:i]valueForKey:#"longitude"] doubleValue];
eventTypeStr = [[MapDataArray objectAtIndex:i]objectForKey:#"is_paid"];
NSString * price = [[MapDataArray objectAtIndex:i] objectForKey:#"evt_price"];
NSString * Mainprice = [NSString stringWithFormat:#"$%#",price];
NSString * name = [[MapDataArray objectAtIndex:i] objectForKey:#"evt_title"];
NSLog(#"Name is %#", name);
for(int i=0;i<[MapDataArray count];i++){
GMSMarker * marker = [[GMSMarker alloc] init];
if ([eventTypeStr isEqualToString:#"0"])
{
NSLog(#"Eventtype Array is %#",eventTypeStr);
NSLog(#"Event Type is %#",eventTypeStr);
marker.icon = [UIImage imageNamed:#"smiley.png"];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = name;
marker.snippet = name;
marker.tappable = YES;
marker.appearAnimation = kGMSMarkerAnimationPop;
//marker.animated = YES;
marker.map = mapView;
[mapView setSelectedMarker:marker];
}
else if ([eventTypeStr isEqualToString:#"1"])
{
NSLog(#"Event Type is %#",eventTypeStr);
//marker.icon = [UIImage imageNamed:#"dollar1.png"];
marker.icon = [self drawText:price inImage:[UIImage imageNamed:#"markertt1.png"]];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = price;
marker.snippet = name;
marker.tappable = YES;
marker.appearAnimation = kGMSMarkerAnimationPop;
//marker.animated = YES;
marker.map = mapView;
[mapView setSelectedMarker:marker];
}
else if ([eventTypeStr isEqualToString:#"2"])
{
NSLog(#"Event Type is %#",eventTypeStr);
marker.icon = [UIImage imageNamed:#"donation.png"];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = name;
marker.snippet = name;
marker.tappable = YES;
marker.appearAnimation = kGMSMarkerAnimationPop;
//marker.animated = YES;
marker.map = mapView;
[mapView setSelectedMarker:marker];
}
}
}
}

I am trying to display multiple markers and a line between them on google map

for (int i=0; i<self.busRoutesArr.count-1; i++)
{
NSString *lat = [self.latArr objectAtIndex:i];
NSString *lon = [self.longArr objectAtIndex:i] ;
double lt=[lat doubleValue];
double ln=[lon doubleValue];
NSLog(#"%f, %f",lt,ln);
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lt
longitude:ln
zoom:60];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectMake(10, 100, 250, 250) camera:camera];
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = camera.target;
marker.snippet = #"Hello World";
marker.map = mapView;
mapView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:mapView];
}
Only 1 marker being displayed. I want to display all the markers and a line connecting them
The following code is for displaying all markers in the map.
for(int i=0;i<[array count];i++) {
GMSMarker *marker = [[GMSMarker alloc] init];
marker.animated=YES;
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.title = #"name";
marker.snippet = #"snippet";
marker.map = mapView;
}

Annotations is not shown on googlemap

Annotations are not shown on map.what could be the reason ? pls help.i cannot see any Pin or Annotations on the map,it is showing area near portugal,spain etc.
addressArray = dictionary[#"data"][#"centers"];
NSArray *latitude =[dictionary[#"data"][#"centers"]valueForKey:#"lat"];
NSArray *longitude=[dictionary[#"data"][#"centers"]valueForKey:#"lng"];
NSString *cname=[dictionary[#"data"][#"centers"]valueForKey:#"category_name"];
loaderView.hidden = true;
NSLog(#"%#",latitude);
NSLog(#"%#",longitude);
NSLog(#"%#",cname);
UIImage *pinImage = [UIImage imageNamed:#"map_gym"];
GMSMarker *mark=[[GMSMarker alloc]init];
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[latitude objectAtIndex:0] doubleValue] ,[[longitude objectAtIndex:0] doubleValue]);
NSLog(#"%#",[latitude objectAtIndex:0]);
NSLog(#"%#",[longitude objectAtIndex:0]);
mark.position =coord;
mark.title=cname;
mark.icon=pinImage;
mark.infoWindowAnchor = CGPointMake(0.5, 0.25);
mark.groundAnchor = CGPointMake(0.5, 1.0);
mark.map=_mapView;
I think the coordinates of the marker is not properly implemented. Take a look at the code sample in Google's Document.
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
marker.title = #"Hello World";
marker.map = mapView_;
Try adjusting your code to this.
E.g.
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[latitude objectAtIndex:0] doubleValue] ,[[longitude objectAtIndex:0] doubleValue]);
GMSMarker *marker = [GMSMarker markerWithPosition:coord];
marker.title = #"Hello World";
marker.map = mapView_;

How to extend polyline of google maps based on gps location latitudes and langitudes dynamically

Initialy created poly line and added some path to it . after creating this polyline i need add some other points based on gps location and drawing polyline again .is that redraw again the path.
_path = [GMSMutablePath path];
[_path addLatitude:12.9716 longitude:77.5946]; // bangalore
[_path addLatitude:13.3710 longitude:76.6413]; // Fiji
[_path addLatitude:15.3173 longitude:75.7139]; // Hawaii
[_path addLatitude:15.3647 longitude:75.1240]; // Mountain View
GMSMarker *marker1 = [[GMSMarker alloc] init];
marker1.position = CLLocationCoordinate2DMake(12.9716, 77.5946);
marker1.title = #"Bangalore";
marker1.groundAnchor = CGPointMake(0.2, 0.9);
marker1.appearAnimation = kGMSMarkerAnimationPop;
marker1.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker1.snippet = #"India";
marker1.map = _mapView;
GMSMarker *marker2 = [[GMSMarker alloc] init];
marker2.position = CLLocationCoordinate2DMake(13.3710, 76.6413);
marker2.title = #"Tumkur";
marker2.groundAnchor = CGPointMake(0.3, 0.9);
marker2.snippet = #"India";
marker2.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker2.map = _mapView;
GMSMarker *marker3 = [[GMSMarker alloc] init];
marker3.position = CLLocationCoordinate2DMake(15.3173, 75.7139);
marker3.title = #"Mysore";
marker3.groundAnchor = CGPointMake(0.3, 0.9);
marker3.snippet = #"India";
marker3.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker3.map = _mapView;
GMSMarker *marker4 = [[GMSMarker alloc] init];
marker4.position = CLLocationCoordinate2DMake(15.3647, 75.1240);
marker4.title = #" Hubli";
marker4.groundAnchor = CGPointMake(0.3, 0.9);
marker4.snippet = #"India";
marker4.icon = [UIImage imageNamed:#"Flag Filled -50.png"];
marker4.map = _mapView;
marker4.rotation = 180;
CLLocationCoordinate2D circleCenter1 = CLLocationCoordinate2DMake(15.3647, 75.1240);
GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:circleCenter1 zoom:10];
[_mapView animateWithCameraUpdate:updatedCamera];
_polyline = [GMSPolyline polylineWithPath:_path];
_polyline.strokeColor = [UIColor blueColor];
_polyline.strokeWidth = 1.f;
_polyline.map = _mapView;
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
static CLLocation *lastKnownLocation = nil;
if(lastKnownLocation == nil) lastKnownLocation = [locations lastObject];
else
{
CLLocation *currentLoc = [locations lastObject];
// Forming GMSPath from User's last location to User's current location.
GMSMutablePath *path = [GMSMutablePath new];
[path addCoordinate:[lastKnownLocation coordinate]];
[path addCoordinate:[currentLoc coordinate]];
GMSPolyline *polyLine = [GMSPolyline polylineWithPath:path];
polyLine.strokeColor = [UIColor redColor];
polyLine.strokeWidth = 2.f;
polyLine.map = googleMap;
// Saving current loc as last known loc,so that we can draw another GMSPolyline from last location to current location when you recieved another callback for this method.
lastKnownLocation = currentLoc;
}
}
Above code will draws path on google map when app got new location update(from second time onward).
Hope that helps,
Get back to me if you need some more deep info.

fitBounds in GoogleMaps SDK for ios does not fit

I want use fitBounds method in GoogleMaps for ios SDK and view does not fit. My varaibles are all right (path, arrays, etc...) because I can see polyline with markers on map. The only thing that does not work is fit to view. Where I have made mistake? thank you.
// Create a 'normal' polyline.
GMSPolyline *polyline = [[GMSPolyline alloc] init];
GMSMutablePath *path = [GMSMutablePath path];
locationInfoArray = [LocationInfoMemoryManager loadLocationDataWithPath:_locationInfoPathString];
for (int i=0; i<locationInfoArray.count; i++) {
LocationInfo* locationInfo = locationInfoArray[i];
CLLocationCoordinate2D locationPoint = {locationInfo.latitude, locationInfo.longitude};
[path addCoordinate:locationPoint];
}
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[locationInfoArray[0] latitude]
longitude:[locationInfoArray[0] longitude]
zoom:5 ];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
polyline.path = path;
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 10.f;
polyline.zIndex = 15; // above the larger geodesic line
polyline.map = mapView;
GMSMarker *startMarker = [[GMSMarker alloc] init];
startMarker.title = #"Start";
startMarker.snippet = #"Info will be here";
startMarker.position = CLLocationCoordinate2DMake([[locationInfoArray firstObject] latitude], [[locationInfoArray firstObject] longitude]);
startMarker.map = mapView;
startMarker.flat = NO;
//sydneyMarker.rotation = 30.0;
mapView.selectedMarker = startMarker;
GMSMarker *finishMarker = [[GMSMarker alloc] init];
finishMarker.title = #"Finish";
finishMarker.snippet = #"Info will be here";
finishMarker.position = CLLocationCoordinate2DMake([[locationInfoArray lastObject] latitude], [[locationInfoArray lastObject] longitude]);
finishMarker.map = mapView;
finishMarker.flat = NO;
mapView.selectedMarker = finishMarker;
//Here is probably problem
GMSCoordinateBounds *bounds; = [[GMSCoordinateBounds alloc] initWithPath:path];
GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds withPadding:20];
[mapView moveCamera:update];
self.view = mapView;
Is this code in loadView or viewDidLoad? Based on these earlier questions, I think camera updates only work correctly from viewWillAppear:
GMSCameraUpdate zooming out to the max distance rather than around a path
Fit bounds not working as expected

Resources