2

I use this code to create marker on Google Map for iOS.

self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    self.mapView.myLocationEnabled = YES;
    self.mapView.accessibilityElementsHidden = NO;
    self.mapView.frame = self.view.bounds;
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.mapView.delegate = self;
    self.mapView.settings.myLocationButton = YES;
    [self.view addSubview:self.mapView];

GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.map = self.mapView;

But I need the marker to look like this:

enter image description here

I know how to do that with Apple Map, but I need to use Google Map (because in some cities Apple Map doesn't show almost anything).

For google map I found only code like this:

marker.icon = image; // image from xib file

So I will need to create image from xib for each marker. I will have a lot of markers, so may be with this approach I will get memory warning.

Do anybody know better way to implement marker like that?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Paul T.
  • 4,938
  • 7
  • 45
  • 93
  • I think you need to implement clusters for many markers. – iphonic May 08 '14 at 08:06
  • can you give any link or code example? Will "clusters for many markers" help to show only view markers or it will help to show custom view in marker? – Paul T. May 08 '14 at 08:24
  • See this http://stackoverflow.com/questions/20175605/marker-clustering-with-google-maps-sdk-for-ios – iphonic May 08 '14 at 08:47

4 Answers4

9

Since GMSMarker only allows an UIImage to be set as icon, I implemented something similar like this: (it's in Swift, but the concept is the same for Objective C)

var gasView = UIView(frame:CGRectMake(0, 0, 30, 37))
//Add Label
var lblPrecio = UILabel(frame: CGRectMake(0, 37-18, 30, 10))
lblPrecio.text = "hello"
lblPrecio.textAlignment = NSTextAlignment.Center
gasView.addSubview(lblPrecio)
//Add Logo
var logo = UIImageView(frame: CGRectMake(30-23, 2, 16, 16))
logo.image = UIImage(named: "Logo")
gasView.addSubview(logo)
//Add Callout Background Image
gasView.backgroundColor = UIColor(patternImage: UIImage(named: "Callout")!)

marker.icon = imageFromView(gasView)

Where the function imageFromView is the following:

private func imageFromView(aView:UIView) -> UIImage {
    if(UIScreen.mainScreen().respondsToSelector("scale")) {
        UIGraphicsBeginImageContextWithOptions(aView.frame.size, false, UIScreen.mainScreen().scale)
    }
    else {
        UIGraphicsBeginImageContext(aView.frame.size)
    }
    aView.layer.renderInContext(UIGraphicsGetCurrentContext())
    var image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}

Hope this helps.

JoeGalind
  • 3,545
  • 2
  • 29
  • 33
0

As far as I know, that is the only way. But you could follow the best practice from Google on how to limit markers on the Google Map to prevent the Memory or Performance issue.. Read https://developers.google.com/maps/articles/toomanymarkers to understand better.

Ricky
  • 10,485
  • 6
  • 36
  • 49
0

Ok follow this example, it is very simple to implement what u want:

  • Declare an uiimageview
  • Declare a second imageview ( that u want appears inside the fiist, in this case the girl in b&w)
  • So add as aa Subview the second image to the first image
  • Now u can add the first image to marker.icon = (HERE THE FIRST IMAGE!!);

Hope this can help u.

0
 Set the color to your marker first and then camera position to that coordinate, So that whenever your app will start it redirect to that coordinate

 marker.icon = [UIColor redColor];
 GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:< CLLocationCoordinate2DMake>];
        [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:10.0f]];
        bounds=nil;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];
            [self.mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:0.0f]];
            bounds=nil;
Darshan Mothreja
  • 506
  • 4
  • 13