GMSMarker not showing on map after adding - ios

I'm use button for adding pin on specific location and my map already load and showing on screen. then press button not response even marker create and assign coordinates as well.
-(IBAction)addingMarkerOnMap:(id)sender
{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"Standard"];
london.map = self.mapView;
}

Try this code:
-(IBAction)addingMarkerOnMap:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"Standard"];
london.map = self.mapView;
});
}

Related

GMSMarker not show on button click run time

I'm try to adding marker on run time then press button. Button method call and code run but no marker show on map. if i add marker on adding subview time then show perfectly but i need to different case on button press.
Button Click (Not Showing any maker on Map)
-(IBAction)addingMarkerOnMap:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"pinIcon"];
london.map = self.mapView;
});
}
Adding Time (Showing marker)
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:self.currentLocation.coordinate.latitude
longitude:self.currentLocation.coordinate.longitude
zoom:16];
self.mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];
self.mapView.myLocationEnabled = YES;
[self.mapView setDelegate:self];
self.mapView.mapType = kGMSTypeSatellite;
[self.googleMapView addSubview:self.mapView];
self.marker = [[GMSMarker alloc] init];
self.marker.position = CLLocationCoordinate2DMake(43.649580, -79.385440);
self.marker.title = #"Toronto";
self.marker.snippet = #"Canada";
self.marker.map = self.mapView;
Please use this code on a demo project then check in your own project
#IBOutlet var mapView: GMSMapView!
//use the following code in you button action
let state_marker = GMSMarker()
let long = //Double long
let lat = //Double lat
state_marker.position = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
state_marker.map = mapView
// add in subview if you want to add, or place a view on story board and assign GMSMapView to this

iOS GMaps removing old GMSMarker from map

I want to add new GMSMarker in the map and remove the old one. Previously I was using [map clear]; method and adding new marker. It was working fine. But I dont want that. I want to add new marker and remove the old marker without clearing the map each time.
My code:
if(markerMYLocation == nil)
{
markerMYLocation = [[GMSMarker alloc] init];
}
markerMYLocation.map = nil;
markerMYLocation.position = CLLocationCoordinate2DMake(latitude_Point, longitude_Point);
markerMYLocation.title = #"You";
markerMYLocation.groundAnchor = CGPointMake(0.5, 0.5);
markerMYLocation.icon = [UIImage imageNamed:#"white.png"];
markerMYLocation.map = mapViewGoog;
Question:
1) What is the correct way of removing and adding the marker?
2)I am initializing marker only in my viewDidLoad. Is that the correct way of doing this or should I initialize each time I add it?
Marker add on Map View
GMSMarker *marker = [GMSMarker markerWithPosition:[marker.getcoordinateObject MKCoordinateValue]];
marker.icon =[UIImage imageNamed:#“”];
marker.title = #“Driver Pin”
marker.map = mapView_;
[allMarkeronMap addObject:googleMapsDriverPin]; // Add maker on Array
[_mapView addSubview:mapView_];
//Make MutableArray on viewDidLoad
NSMutableArray * allMarkeronMap =[NSMutableArray alloc] init];
particualr marker or old marker remove on mapView
for (GMSMarker *pin in allMarkeronMap) {
if (pin.userData == #"Driver Pin"){
pin.map = nil;
}
}

My end point GMSMarker do not show on google map view

I have implemented GMSMarker, in that my end point GMSMarker do not show on google map view.
- (GMSMarker *)markerWithPath:(CLLocationCoordinate2D *)path Index:(NSUInteger )index IconName:(NSString *)imageName {
GMSMarker *marker = [GMSMarker markerWithPosition:path[index]];
marker.icon = [UIImage imageNamed:imageName];
marker.flat = YES;
return marker;
}

GMSMarker show custom uiview as a maker

want to show this kind on pin on google map, how to achieve this.
From the Documentation:
Change the default marker icon
If you want to change the default marker image you can set a custom
icon. Custom icons are always set as a UIImage object. The following
snippet creates a marker with a custom icon centered at London,
England. The snippet assumes that your application contains an image
named "house.png".
For more:
OBJECTIVE-C
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *london = [GMSMarker markerWithPosition:position];
london.title = #"London";
london.icon = [UIImage imageNamed:#"house"];
london.map = mapView_;
I had to do something similar for selected and unselected markers. But the idea is still the same with what you want to do.
Assuming you have a parkingObject model, when plotting the markers:
-(void) plotMarkers{
for (ParkingObject *parkingMarker in parkingArray){
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = parkingMarker.position;
marker.userData = parkingMarker;
marker.icon = [self createMarker:marker withImageName:#"markerIcon.png"];
marker.infoWindowAnchor = CGPointMake (0.5, 1);
marker.map = mapView_;
}
}
create custom marker with a different image and data:
- (UIImage*) createMarker: (GMSMarker* )marker withImageName:(NSString* )imageName{
ParkingObject *parkingObject = marker.userData;
if([imageName isEqualToString:#"markerSelected.png"]){
MarkerSelected * infoWindow = [[[NSBundle mainBundle]loadNibNamed:#"MarkerSelected" owner:self options:nil]objectAtIndex:0];
infoWindow.markerImage.image = [UIImage imageNamed:imageName];
return [self imageFromView:infoWindow];
}
else{
Marker * infoWindow = [[[NSBundle mainBundle]loadNibNamed:#"Marker" owner:self options:nil]objectAtIndex:0];
infoWindow.priceLabel.text = [NSString stringWithFormat:#"$%.0f",parkingObject.rate.floatValue];
infoWindow.markerImage.image = [UIImage imageNamed:imageName];
return [self imageFromView:infoWindow];
}
}
You'll need to create a marker.xib that has the images/icon/text properties that you can set
this is just sample code, you'll need to customize it for your needs.

GMSMapView GroundOverlay Cover Entire MapView Bounds

I have setup a UIView to display a specific region of a Map using Google Maps. I now want to add an image overlay on top of this selected region but I dont know how to calculate the correct coordinates for this. I have set the map with a center coordinate, but the Overlay needs NorthWest and South East coordinates. Can someone help please? I am trying to put an image of a race track over some roads.
Below is my code so far:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:14.5809268
longitude:120.975319
zoom:16.5
bearing:90
viewingAngle:0];
// Indicating the map frame bounds
mapView_ = [GMSMapView mapWithFrame:self.mapViewOnScreen.bounds camera: camera];
mapView_.myLocationEnabled = YES;
// Add as subview the mapview
[self.mapViewOnScreen addSubview: mapView_];
//this is where I need to figure out the coordinates but get stuck...
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(40.712216,-74.22655);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(40.773941,-74.12544);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest
coordinate:northEast];
//Add track image over the road map to show a race track - roads need to match up
UIImage *icon = [UIImage imageNamed:#"Track.jpg"];
GMSGroundOverlay *overlay =
[GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:icon];
overlay.bearing = 0;
overlay.map = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(14.5809268, 120.975319);
marker.title = #"Race Day";
marker.snippet = #"Manila";
marker.map = mapView_;
}
I haven't tried this, but after doing some research on the API, I think you can achieve what you want by doing the following:
GMSProjection *projection = mapView_.projection;
GMSVisibleRegion visibleRegion = [projection visibleRegion];
GMSCoordinateBounds *coordinateBounds = [[GMSCoordinateBounds alloc] initWithRegion:visibleRegion];
CLLocationCoordinate2D northEast = coordinateBounds.northEast;
CLLocationCoordinate2D southWest = coordinateBounds.southWest;
Hope this helps.

Resources