How to make custom UIView OR label on GMSMarker Google - ios

I have to add a custom label on Marker pin of Google maps in IOS.
I have already implemented markerInfoWindow method. But it works after i tapped on marker/Pin.
I need to show custom label at start as long as mapview will load on view without performing any action. As we can set icon or title on the marker, I need to show my own Custom view on it.
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
CLLocationCoordinate2D anchor = marker.position;
// CGPoint point = [mapView.projection pointForCoordinate:anchor];
// self.calloutView.title = marker.title;
[self.calloutView setFrame:CGRectMake(0, 50, 25, 25)];
self.calloutView.layer.cornerRadius = 11.0;
self.calloutView.layer.masksToBounds = YES;
self.calloutView.layer.borderColor = [UIColor clearColor].CGColor;
self.calloutView.layer.borderWidth = 0.0;
self.calloutView.hidden = NO;
return self.calloutView;
}

Use this code to add an image for GMSMarker:
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = MARKER_POSITION;
marker.infoWindowAnchor = CGPointMake(0.44f, 0.45f);
marker.icon = [UIImage imageNamed:#"CustomMarkerImageName"];
You can also use this delegate method to provide custom view for additional info:
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
InfoWindow *view = [[[NSBundle mainBundle] loadNibNamed:#"InfoWindow" owner:self options:nil] objectAtIndex:0];
view.name.text = #"Place Name";
view.description.text = #"Place description";
view.phone.text = #"123 456 789";
view.placeImage.image = [UIImage imageNamed:#"customPlaceImage"];
view.placeImage.transform = CGAffineTransformMakeRotation(-.08);
return view;
}
Check this answer as a reference: https://stackoverflow.com/a/16767124/2082569

If I understand correctly, the ask is to show the info window for a particular marker programmatically.
[self.mapView setSelectedMarker:marker];
To achieve camera refocus similar to if a user had tapped the marker, do something like:
GMSCameraPosition *cameraPosition =
[[GMSCameraPosition alloc] initWithTarget:marker.position
zoom:15
bearing:0
viewingAngle:0];
[self.mapView animateToCameraPosition:cameraPosition];

Related

How to remove the Blue and gray annotions of mapbox in iOS

How Can I Remove the user location icon defaultly shown by the map box
- (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAnnotation>)annotation{
if (mapView.userTrackingMode == MGLUserTrackingModeFollow) {
return nil;
}
return Nil;
}
Hi, I have implemented this code.And I viewdidloadTried this
mapView.tintColor = [UIColor clearColor];
mapView.backgroundColor = [UIColor clearColor];
[mapView.attributionButton setTintColor:[UIColor clearColor]];
But still Showing ...
Mapbox documentation
you have configured your application’s location permissions, display the device’s current location on the map by setting the showsUserLocation property on the map view to NO
- (void)viewDidLoad {
[super viewDidLoad];
MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[mapView setCenterCoordinate:CLLocationCoordinate2DMake(40.74699, -73.98742)
zoomLevel:9
animated:NO];
[self.view addSubview:mapView];
mapView.styleURL = [MGLStyle satelliteStreetsStyleURL];
// Add a point annotation
MGLPointAnnotation *annotation = [[MGLPointAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(40.77014, -73.97480);
annotation.title = #"Central Park";
annotation.subtitle = #"The best park in New York City!";
[mapView addAnnotation:annotation];
// Set the map view's delegate
mapView.delegate = self;
// Allow the map view to display the user's location
mapView.showsUserLocation = NO;
}
If you check the documentation, you'll find that mapView.showsUserLocation = false should do the trick.

custom frame to annotation view mapbox not working

Im using mapbox and adding a custom annotation view to it. But somehow the annotation view's height is not reflecting.
Here's the code for it.
- (void)viewDidLoad {
[super viewDidLoad];
MGLMapView *mapView = [[MGLMapView alloc] initWithFrame:self.view.bounds];
mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(38.894368, -77.036487);
point.title = #"Hello world!";
point.subtitle = #"Welcome to The Ellipse.";
mapView.delegate = self;
[mapView setCenterCoordinate:point.coordinate zoomLevel:12 animated:NO];
[mapView addAnnotation:point];
[self.view addSubview:mapView];
// Do any additional setup after loading the view, typically from a nib.
}
-(UIView *)mapView:(MGLMapView *)mapView leftCalloutAccessoryViewForAnnotation:(id<MGLAnnotation>)annotation
{
customAnnotationView *view = [[[NSBundle mainBundle] loadNibNamed:#"customAnnotationView" owner:self options:nil] objectAtIndex:0];
[view setFrame:CGRectMake(0, 0, 260, 100)];
return view;
}
Here the height of view is not reflecting on map.
SMCalloutView, which the Mapbox iOS SDK uses to provide annotation callouts, doesn't dynamically resize based on its child views' height. (At least, it doesn't with how Mapbox configures SMCalloutView by default).
More callout view customization is coming with v3.1.0, though!
mapView:calloutViewForAnnotation: and MGLCalloutView will allow you to completely customize the appearance.

iOS Mapbox Custom Marker Will Not Appear

Custom Marker Image will not appear only default marker appears in it's place.
In ViewWillAppear:
RMPointAnnotation *annotation3 = [[RMPointAnnotation alloc] initWithMapView:parkMap coordinate:coordinate andTitle:title];
[parkMap addAnnotation:annotation3];
- (RMMapLayer *)mapView:(RMMapView *)mapView layerForAnnotation:(RMAnnotation *)annotation
{
if (annotation.isUserLocationAnnotation)
return nil;
RMMarker *marker;
marker = [[RMMarker alloc] initWithUIImage:[UIImage imageNamed:#"square_small.png"]];
marker.canShowCallout = YES;
return marker;
}
Check the docs and don't use RMPointAnnotation.
If you wish to customize the layer appearance in more detail, you should instead create an RMAnnotation and configure its layer directly. Providing a layer manually for instances of RMPointAnnotation will not have any effect.
https://github.com/mapbox/mapbox-ios-sdk/blob/509fa7df46ebd654d130ab2f530a8e380bf2bd59/MapView/Map/RMPointAnnotation.h#L33

How to show all Info window in iOS Google maps without tapping on Marker?

I'm trying to create markers without tapping. But i cant display all infoWindows. It only show one infowindow on last marker.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *markersArray = [[NSMutableArray alloc] init];
for(int i=0; i<10; i++){
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(latitude, longitude);
marker.appearAnimation=YES;
marker.opacity = 0.0;
mapView.selectedMarker = marker;
marker.map = mapView;
[markersArray addObject:marker];
}
}
and custom Infowindow:
- (UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
CustomInforwindow *customView = [[[NSBundle mainBundle] loadNibNamed:#"CustomInforwindow" owner:self options:nil] objectAtIndex:0];
return customView;
}
you can display one InfoWindow at a time.
mapView.selectedMarker = marker; this will open the infowindow for the last marker
If you want to show multiple markers then you should make marker that contains both the marker and the info window .
Hope this helps.
Place two markers at same place (do maintain some gap in between both) and remove interaction for both. For example:
marker1.position = Locationcoorsinate2dobject;
marker2.position = Locationcoorsinate2dobject;
marker1.tappable = false;
marker2.tappable = false;
Now the magic is gonna happen:
marker2.icon = [UIImage imageNamed:#"yourinfowindow.png"];
marker2.groundanchor = CGPointMake(marker1.groundanchor.x, marker1.gorundanchor.y + 2‌​.7);
Hope this helps Happy coding :)

coloring and adding buttons to annotations in a mutable array in iOS

I am coding an application that uses a mapView in iOS.
I am trying to color the annotations in the mutable array to purple and to add a disclosure button to each pin .
The reason that I am using a mutable array is that I am going to retrieve many places from a DB to view each place on the map with a pin.
My code is :
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
//code of map
[mapMKMapView setMapType:MKMapTypeStandard];
[mapMKMapView setZoomEnabled:YES];
[mapMKMapView setScrollEnabled:YES];
MKCoordinateRegion newRegion = { {0.0, 0.0},{0.0, 0.0}};
newRegion.center.latitude = 12.968427;
newRegion.center.longitude = 44.997704;
newRegion.span.latitudeDelta = 0.004731;
newRegion.span.longitudeDelta = 0.006952;
[self.mapMKMapView setRegion:newRegion animated:YES];
//multiple annotations
NSMutableArray *locations = [[NSMutableArray alloc] init ];
CLLocationCoordinate2D loc;
annotationClass *myAnn;
//1st annotation
myAnn = [[annotationClass alloc] init];
loc.latitude = 12.968427;
loc.longitude = 44.997704;
myAnn.coordinate = loc;
myAnn.title = #"Nakheel2";
myAnn.subtitle = #"This Al-Nakheel 2 stage";
[locations addObject:myAnn];
//2nd annotaion
myAnn = [[annotationClass alloc] init];
loc.latitude = 12.971532;
loc.longitude = 44.998015;
myAnn.coordinate = loc;
myAnn.title = #"Nakheel21";
myAnn.subtitle = #"This Al-Nakheel 2 stage Hi";
[locations addObject:myAnn];
[self.mapMKMapView addAnnotations:locations];
}
//******
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)locations
{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:locations reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *adverButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[adverButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = adverButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop = TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
The viewForAnnotation delegate method will be called by the map view automatically (you don't explicitly "run" it).
The most likely reason the map view is not calling it is that the map view's delegate is not set.
In the storyboard or xib, make sure the map view's delegate outlet is connected to the view controller (right-click or ctrl-click the map view and connect the delegate outlet to the view controller).
Or, you can add this line in the code in viewDidLoad before the setMapType:
//code of map
mapMKMapView.delegate = self; // <-- add this line
[mapMKMapView setMapType:MKMapTypeStandard];
An unrelated point:
Instead of using a custom action method for the button, I suggest using the map view's own calloutAccessoryControlTapped delegate method. In that method, you'll get direct access to the annotation object that was tapped (via view.annotation). Remove the addTarget and the button: method if you decide to use the delegate method.

Resources