iOS GMaps removing old GMSMarker from map - ios

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;
}
}

Related

GMSMarker not showing on map after adding

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;
});
}

iOS Google Maps, different custom images for Clusters and individual markers

I am using Google Maps SDK in my iOS app. I am populating the map using the clustering methods.
I have set custom images for the different clustering buckets ex. 10,20...
The individual markers however have the default (google maps red marker icon).
I would like a custom icon for clustering and a different one for single markers.
However inside the methods that render the Cluster that add markers, if you set the marker icons it changes all of the images not just singles.
How do I set different icons for singles and clusters?
this adds the items to clusterManager
id<GMUClusterItem> item =
[[POIItem alloc] initWithPosition:CLLocationCoordinate2DMake([bay.latitude doubleValue], [bay.longitude doubleValue]) name:bay.name status:bay.marker_status];
[clusterManager addItem:item];
Here I add the icons for the cluster buckets
- (id<GMUClusterIconGenerator>)iconGeneratorWithImages {
return [[GMUDefaultClusterIconGenerator alloc] initWithBuckets:#[ #10, #50, #100, #200, #1000 ]
backgroundImages:#[
[UIImage imageNamed:#"big_parking_pin_img"],
[UIImage imageNamed:#"big_parking_pin_img"],
[UIImage imageNamed:#"big_parking_pin_img"],
[UIImage imageNamed:#"big_parking_pin_img"],
[UIImage imageNamed:#"big_parking_pin_img"]
]];
}
This is where the google cluster class adds markers
- (void)renderCluster:(id<GMUCluster>)cluster animated:(BOOL)animated {
float zoom = _mapView.camera.zoom;
if ([self shouldRenderAsCluster:cluster atZoom:zoom]) {
CLLocationCoordinate2D fromPosition;
if (animated) {
id<GMUCluster> fromCluster =
[self overlappingClusterForCluster:cluster itemMap:_itemToOldClusterMap];
animated = fromCluster != nil;
fromPosition = fromCluster.position;
}
UIImage *icon = [_clusterIconGenerator iconForSize:cluster.count];
GMSMarker *marker = [self markerWithPosition:cluster.position
from:fromPosition
userData:cluster
clusterIcon:icon
animated:animated];
[_markers addObject:marker];
} else {
for (id<GMUClusterItem> item in cluster.items) {
CLLocationCoordinate2D fromPosition;
BOOL shouldAnimate = animated;
if (shouldAnimate) {
GMUWrappingDictionaryKey *key = [[GMUWrappingDictionaryKey alloc] initWithObject:item];
id<GMUCluster> fromCluster = [_itemToOldClusterMap objectForKey:key];
shouldAnimate = fromCluster != nil;
fromPosition = fromCluster.position;
}
GMSMarker *marker = [self markerWithPosition:item.position
from:fromPosition
userData:item
clusterIcon:nil
animated:shouldAnimate];
[_markers addObject:marker];
[_renderedClusterItems addObject:item];
}
}
[_renderedClusters addObject:cluster];
}
// Returns a marker at final position of |position| with attached |userData|.
// If animated is YES, animates from the closest point from |points|.
- (GMSMarker *)markerWithPosition:(CLLocationCoordinate2D)position
from:(CLLocationCoordinate2D)from
userData:(id)userData
clusterIcon:(UIImage *)clusterIcon
animated:(BOOL)animated {
CLLocationCoordinate2D initialPosition = animated ? from : position;
GMSMarker *marker = [GMSMarker markerWithPosition:initialPosition];
marker.userData = userData;
if (clusterIcon != nil) {
marker.icon = clusterIcon;
marker.groundAnchor = CGPointMake(0.5, 0.5);
}
marker.map = _mapView;
if (animated) {
[CATransaction begin];
[CATransaction setAnimationDuration:kGMUAnimationDuration];
marker.layer.latitude = position.latitude;
marker.layer.longitude = position.longitude;
[CATransaction commit];
}
return marker;
}
I had the similar problem 2 days ago and I just found the solution. Hope it will be useful for you.
For example you have a mapView and you set a delegate to it in right place:
[self.mapView setDelegate:self];
Then you need to implement the optional method from GMSMapViewDelegate protocol:
- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {
[self performSelector:#selector(updateMarkers) withObject:nil afterDelay:0.2];
}
I use delay 0.2 seconds, because markers wouldn't update their icons if you'll use smaller value.
The next step is implement method for updating icons:
-(void) updateMarkers {
// "mapView" property in your self.mapView has type GMSVectorMapView,
//and it is hidden, so you can't get like self.mapView.mapView
id vectorMap = [self.mapView valueForKey:#"mapView"];
// "accessibilityItems" - property that have all items in visible part of map.
NSMutableArray* GMSMarkersArray = [vectorMap mutableArrayValueForKey:#"accessibilityItems"];
// Very often you'll get object of GMSPointOfInteretUIItem class, and you don't need it =)
NSMutableArray *discardedItems = [NSMutableArray array];
for (id item in GMSMarkersArray) {
if (![item isKindOfClass:[GMSMarker class]])
[discardedItems addObject:item];
}
[GMSMarkersArray removeObjectsInArray:discardedItems];
// If marker don't have icon image, he use default red pin, but property is still have nil-value ...
NSPredicate* predicate = [NSPredicate predicateWithFormat:#"icon = nil"];
NSArray* singleMarkers = [GMSMarkersArray filteredArrayUsingPredicate:predicate];
// ... and here you can setup any icon you want, for all singles markers in visible part of map.
for(GMSMarker* marker in singleMarkers) {
marker.icon = [UIImage imageNamed:#"yourIcon.png"];
}
}
Also if you create your own marker and add it to cluster, you can get it from userData property of GMSMarker object in last loop. And for example you have there your custom marker with icon you want, just change last loop for something like:
for(GMSMarker* marker in singleMarkers) {
YourMarkerClass* yourMaker = marker.userData;
marker.icon = yourMaker.icon;
}
Sorry for possible mistakes and ask the questions if you don't understand something =)

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.

ios google maps performselector to show infowindow

I'm not sure if this is possible with Google Maps for iOS but I'm trying to refresh the open infoWindow of a marker when modal view controller is dismissed. Right now, I'm just trying to get the infoWindow to show up manually. I added a navigationItem button and sending the coordinates to the selector:
- (void) dosomething:(id)sender{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(41.05061, 28.77244);
GMSMarker *marker = [[GMSMarker alloc]init];
marker.position = position;
[self performSelector:#selector(mapView:markerInfoWindow:) withObject:mapView_ withObject:marker];
}
- (UIView *) mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
NSLog(#"%f, %f", marker.position.latitude, marker.position.longitude);
InfoWindow *infoWindow = [[InfoWindow alloc]init];
NSDictionary *thisMarker = [NSDictionary new];
_thisMarker = thisMarker;
for (NSDictionary *dic in [MainMarkers sharedInstance].mainMarkers){
if ([[dic valueForKey:#"latitude"]isEqualToString:#(marker.position.latitude).stringValue] && [[dic valueForKey:#"longitude"]isEqualToString:#(marker.position.longitude).stringValue]) {
_thisMarker = dic;
}
}
NSLog(#"%#", _thisMarker);
//...infoWindow setUp
return infoWindow;
}
The logs work, I'm sending the specified coordinates but the infoWindow doesn't show up. Everything works if I tap on the marker. Is it possible to open the infoWindow this way?
UPDATE:
In the viewWillAppear method, I tried this:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
if (_thisMarker) {
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[_thisMarker valueForKey:#"latitude"] floatValue], [[_thisMarker valueForKey:#"longitude"] floatValue]);
GMSMarker *marker = [[GMSMarker alloc]init];
marker.position = position;
mapView_.selectedMarker = (GMSMarker*)marker;
}
}
I'll add that I'm using a custom view for the infoWindow. If I add marker.map=mapView_; the infoWindow comes up but it doesn't get re-drawn, recreated based on the data (marker icon, marker name, marker details)...so I still can't get it to work.
May want to try this...
[self.mapView setSelectedMarker:marker];
If you already have a marker on the map, you can just ask the map to select it:
self.mapView.selectedMarker = (GMSMarker*)marker;
The result is the same as if the user had tapped on the marker.

add Marker on the Map using Mappy SDK IOS

i'm developing and iphone app and i'm working with Mappy SDK IOS.
i arrived to display the map and show my current location. but i have a problem with adding some markers on my map.
i have an NSArray that contains some objects with longitude and latitude properties.
my code to display the markers is :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//initialisation mot de passe + user
[MPInit initialisation:MAPPY_API_KEY];
[RMMapView class];
//show user location
self.mapView.showsUserLocation = NO;
self.mapView.showPoi = YES;
//set delegate
self.mapView.delegate = self;
MPMarkerManager *markerManager = [self.mapView markerManager];
MPMarkerCategory * category = [markerManager getCategory:#"storeLocations"];
//remove old elements
[category.dataSource removeAllElements];
//add markers on map
for(int i=0; i<self.arrayStores.count; i++) {
NSLog(#"add store %d on the map", i);
NSDictionary* currentStore = [self.arrayStores objectAtIndex:i];
float latitude = [[currentStore objectForKey:#"latitude"] floatValue];
float longitude = [[currentStore objectForKey:#"longitude"] floatValue];
CLLocationCoordinate2D locationStore = CLLocationCoordinate2DMake(latitude, longitude);
//create a new POI object with location
MPPoi * poi = [[MPPoi alloc] initWithUId:[currentStore objectForKey:#"title"] withLocation:locationStore];
[category.dataSource addElement:poi];
self.mapView.centerCoordinate = locationStore;
[poi release];
}
//category settings
category.markerColor = [UIColor greenColor];
//[category setOptimalZoom:YES];//set the zoom to be optimal : show all poi in the category
[category setHideLabelOnTheFirstShow:NO];
[category setAnimateAtFirstShow:YES];
}
and here is the log displayed on the console:
2012-06-14 17:01:18.359 Koutoubia[609:607] MappyKit version:1.40
2012-06-14 17:01:18.672 Koutoubia[609:607] add store 0 on the map
i'm i doing something wrong ?? because markers aren't displayed
the doc for adding markers on the documentation is :
//add a marker on map
MPMarkerManager *markerManager = [viewController.mapView markerManager];
MPMarkerCategory * category = [markerManager getCategory:HMP_LOC_CATEGORY];
//remove old elements
[category.dataSource removeAllElements];
//create a new POI object with location
MPPoi * poi = [[MPPoi alloc] initWithUId:locationData.address withLocation:findLocation];
[category.dataSource addElement:poi];
[poi release];
the link of the documentation is here
any ideas ?? thanks in advance
I've founded the solution ,
the problem is that i've forgot to initialize the MPMarkerCategory and add it to the markerManager on the viewDidLoad method :
//Marker initialization
MPMarkerManager *markerManager = [self.mapView markerManager];
//add a new category with green default color
[markerManager addCategory:STORE_LOC_CATEGORY withColor:[UIColor greenColor]];

Resources