add Marker on the Map using Mappy SDK IOS - 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]];

Related

how to set marker at my current location and change camera position in Baidu Map (objective c)

I configure Baidu map in my code using pod 'BaiduMapKit' and display map in view.
how to set a marker at my current location in Baidu Map and change camera position also?
// now I solve how to set marker but actually my camera location not move.
- (void)viewDidLoad {
[super viewDidLoad];
anotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 22.2593;
coor.longitude = 70.7777;
anotation.coordinate = coor;
anotation.title = #"this is rajkot";
[mapView addAnnotation:anotation];
mapView.delegate = self;
}
CLLocationCoordinate2D coor;
coor.latitude = 22.2593;
coor.longitude = 70.7777;
mapView.centerCoordinate = coor
try this to change the camera
Here you are
- (void)viewDidLoad {
[super viewDidLoad];
anotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 22.2593;
coor.longitude = 70.7777;
anotation.coordinate = coor;
anotation.title = #"this is rajkot";
[mapView addAnnotation:anotation];
mapView.centerCoordinate = coor;
mapView.delegate = self;
}

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 =)

Is there a way to detect if a marker is present on the google map to delete it?

I'm using Google Maps SDK to write an app in Xcode and I have markers all over my map showing different locations.
I have a UIPickerView that shows markers depending on which the user selects like so:
Snippet of code:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (row)
{
case 0: // Display no marker flags
{
....
}
break;
case 1: // Display vendor marker flags
{
if (isSelectedVendors == NO)
{
// Mark item as selected in picker list
isSelectedVendors = YES;
[self addVendorMarkersToMap];
}
break;
}
....
}
Snippet of method to load markers on map:
- (void)addVendorMarkersToMap
{
// Add coordinates to know where to place markers
NSArray *coords = [ [NSArray alloc] initWithObjects: ...];
// Loop through all the coordinates and mark it on the map
for (int i = 0; i < [coords count]; i = i + 2)
{
GMSMarker *marker = [ [GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake([ [coords objectAtIndex:i] floatValue], [ [coords objectAtIndex: (i + 1)] floatValue]);
marker.appearAnimation = kGMSMarkerAnimationPop;
marker.icon = [UIImage imageNamed:#"vendors.png"];
marker.map = mapView;
}
}
Google Map's documentation doesn't explain how to detect markers on the map.
Since I'm using a UIPickerView to display certain markers on the map, how do I detect markers on the map to delete them and only show the markers the user selects from the UIPickerView?
The only thing I know to do is to use [mapView clear] but then that would clear everything on the map, even the overlay that I have over the map.

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.

MKMapView freezes on User Interaction

I have an application where I show a small map with one annotation, the map is in a objectDetailScreen that I created, this screen is updated whenever a new object is set, also showing a new map. The map as a annotation for the object.
Whenever I try to move the view, tap the annotations pin or zoom, the map freezes for a while and I have no clue why it does that. The app is iPad only (iPad 2, iOS 4.3.5). Here is the code that sets the map:
- (void) setObject:(AchmeaObject *)_object
{
if(kaart != nil)
{
[kaart removeFromSuperview];
kaart = nil;
}
kaart = [[MKMapView alloc] initWithFrame:CGRectMake(340, 380, 400,300)];
[kaart setDelegate: self];
[kaart setUserInteractionEnabled:YES];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [_object.latitude doubleValue];
coordinate.longitude = [_object.longitude doubleValue];
double miles = 2;
double scalingFactor = ABS( cos(2 * M_PI * coordinate.latitude /360.0) );
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor*69.0);
MKCoordinateRegion region;
region.span = span;
region.center = coordinate;
[kaart setRegion: region animated:YES];
ObjectAnnotation *sa = [[ObjectAnnotation alloc] initWithName: _object.plaats Address: _object.adres Coordinate:coordinate];
NSArray *anotations = [NSArray arrayWithObject: sa];
[kaart addAnnotations:anotations];
[self.view addSubview:kaart];
}
I have no idea why it happens, but when it first shows it takes a few seconds to respond to any user interaction, and after every interaction it needs at least a few more seconds, until after a few times freezing completely.
ObjectAnnotation.m
#import "ObjectAnnotation.h"
#implementation ObjectAnnotation
#synthesize coordinate = _coordinate;
- (id) initWithName: (NSString *) _name Address: (NSString *) _address Coordinate: (CLLocationCoordinate2D) _coord{
self = [super init];
name = [_name retain];
address = [_address retain];
_coordinate = _coord;
return self;
}
- (NSString *)title {
return name;
}
- (NSString *)subtitle {
return address;
}
- (void)dealloc
{
[name release];
name = nil;
[address release];
address = nil;
[super dealloc];
}
#end
I suspect this is related to re-adding new map views. Add a map view in the xib or very early in the view controller's life cycle and keep it hidden, and then show it and update its position (with setRegion:animated:). Creating a new map view each time is both expensive and unnecessary; if you're doing it because you want to get rid of some state (annotations, the map's region), learn how to reset that state in the existing map view instead.
It could also be related to your ObjectAnnotation, to which you do not provide the source code.
I suffered the same problem. After profiling, I find out tint property could be causing the map refresh delay. So I set default tint at storyboard and problem disappeared.

Resources