G'day All,
I am trying to change the colours of the pins in MKPointAnnotations. Most of the code examples I have tried to follow are too complex for me to follow. I have come up with this:
//
// ViewController.m
// PlayingWithMaps
//
// Created by custom on 22/09/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
static NSString *annotationIdentifier = #"annotationIdentifier";
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
pinView.pinColor = MKPinAnnotationColorPurple;
return pinView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// First, choose what type of map
//mapView.mapType = MKMapTypeHybrid;
mapView.mapType = MKMapTypeStandard;
// Second, where is the centre of the map
CLLocationCoordinate2D centreCoord = {.latitude = -37.123456, .longitude = 145.123456};
MKCoordinateSpan mapSpan = {.latitudeDelta = 0.001, .longitudeDelta = 0.001};
MKCoordinateRegion region = {centreCoord, mapSpan};
[mapView setRegion:region];
// Now lets make a single annotation.
CLLocationCoordinate2D annotationCoordinate;
annotationCoordinate.latitude = -37.781650;
annotationCoordinate.longitude = 145.076116;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationPoint.coordinate = annotationCoordinate;
annotationPoint.title = #"My point of interest";
annotationPoint.subtitle = #"Location";
[mapView addAnnotation:annotationPoint];
// Read a set of points from files into arrays
NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"latitudes" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *latitudeStringsArray = [NSMutableArray arrayWithArray:[fileString componentsSeparatedByString:#"\n"]];
fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"longitudes" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil];
NSMutableArray *longitudeStringsArray = [NSMutableArray arrayWithArray:[fileString componentsSeparatedByString:#"\n"]];
// Now lets put them onto the map
int i; // Loop control
for (i=0; i<25; i++)
{
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init];
annotationCoordinate.latitude = [[latitudeStringsArray objectAtIndex:i]floatValue];
annotationCoordinate.longitude = [[longitudeStringsArray objectAtIndex:i]floatValue];
annotationPoint.coordinate = annotationCoordinate;
annotationPoint.title = [NSString stringWithFormat:#"Point %d",i];
annotationPoint.subtitle = [NSString stringWithFormat:#"%f, %f",[[latitudeStringsArray objectAtIndex:i]floatValue],[[longitudeStringsArray objectAtIndex:i]floatValue]];
[mapView addAnnotation:annotationPoint];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
The points appear as in the right places but they are the default red and not purple as I was trying to achieve. My understanding from the reading I have done is that the (MKAnnotationView) method should be called whenever an annotation point is created but it's not happening.
I know it's something fundamental that I'm missing but I have no idea what.
All assistance greatly appreciated.
Cheers,
If it's not called then you haven't set your ViewController as delegate for map view.
Related
I am trying to annotate some locations in map using MapBox. and its done.
But the problem is some other annotations are seen in map without any reference, i.e we cant click on it and when i zoom in/zoom out it disappears.
Am only try to create two annotations including user location.
How this happen?
is it because any reusing of annotation?
Some code snippets used:
1) creating annotation in loop
MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = coordinate;
point.title = location;
NSString *altString =[NSString stringWithFormat:#"%#",mIsNSDictionary(response)?[response objectForKey:kKeyAlt]:response];
NSString *str = [NSString stringWithFormat:kKeyLocationLatLonNAltInBaidu,coordinate.latitude,coordinate.longitude,[altString floatValue]];
point.subtitle = str;
[self.arrayAnnotations addObject:point];
[self.mapView addAnnotation:point];
Note: self.arrayAnnotations contains only 2 points
2) Delegate method for annotation
-(MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id <MGLAnnotation>)annotation {
if([annotation isKindOfClass:[MGLPointAnnotation class]] && [self.arrayAnnotations containsObject:annotation]) {
NSString *reuseIdentifier = [self makeIdentifierString:annotation];
MGLAnnotationImage *annotationImage = [mapView dequeueReusableAnnotationImageWithIdentifier:reuseIdentifier];
if (!annotationImage) {
annotationImage = [MGLAnnotationImage annotationImageWithImage:[UIImage imageNamed:kImageNameMarker] reuseIdentifier:reuseIdentifier];
}
return annotationImage;
}
return nil;
}
Is there any way to change default marker icon in marker clustering?
Here is my code...
- (void)viewDidLoad {
[super viewDidLoad];
// Set up the cluster manager with a supplied icon generator and renderer.
id<GMUClusterAlgorithm> algorithm = [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
id<GMUClusterIconGenerator> iconGenerator = [[GMUDefaultClusterIconGenerator alloc] init];
id<GMUClusterRenderer> renderer = [[GMUDefaultClusterRenderer alloc] initWithMapView:googleMapView
clusterIconGenerator:iconGenerator];
clusterManager = [[GMUClusterManager alloc] initWithMap:googleMapView
algorithm:algorithm
renderer:renderer];
// Register self to listen to both GMUClusterManagerDelegate and
// GMSMapViewDelegate events.
[clusterManager setDelegate:self mapDelegate:self];
}
- (void)loadView {
// Create a GMSCameraPosition that tells the map to display the
_camera = [GMSCameraPosition cameraWithLatitude:29.3117
longitude:47.4818
zoom:8];
googleMapView = [GMSMapView mapWithFrame:CGRectZero camera:_camera];
googleMapView.myLocationEnabled = YES;
googleMapView.settings.compassButton = YES;
googleMapView.settings.myLocationButton = YES;
googleMapView.delegate = self;
self.view = googleMapView;
}
-(void)setLocation:(CLLocation *)location
{
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[googleMapView animateToLocation:center];
[googleMapView animateToZoom:12];
NSMutableArray *array = [NSMutableArray arrayWithObjects:
#"29.0827,48.1363",
#"29.2679,47.9927",
#"29.348706, 48.092425",
#"29.340925, 48.088477",
#"29.324912, 48.089850",
#"29.330599, 47.990630",
#"29.300364, 47.960589",
#"29.271917, 47.918017",
#"29.3032,47.936", nil];
//remove all clusters before adding clusters
[clusterManager clearItems];
for (int i = 0; i < [array count]; i++)
{
center = CLLocationCoordinate2DMake ([[array[i] componentsSeparatedByString:#","][0] floatValue], [[array[i] componentsSeparatedByString:#","][1] floatValue]);
// Add items to the cluster manager.
NSString *name = nil;//[NSString stringWithFormat:#"Item %d", i];
id<GMUClusterItem> item =[[POIItem alloc] initWithPosition:center
name:name];
[clusterManager addItem:item];
}
// Call cluster() after items have been added
// to perform the clustering and rendering on map.
[clusterManager cluster];
}
Please guide me...
I see that you used google-maps-ios-utils. The problem is that there is no API for change marker's icon yet. You can only do it directly in the code of the library. I've pasted my custom code inside the method
- (GMSMarker *)markerWithPosition:(CLLocationCoordinate2D)position
from:(CLLocationCoordinate2D)from
userData:(id)userData
clusterIcon:(UIImage *)clusterIcon
animated:(BOOL)animated{
//...
if (clusterIcon != nil) {
marker.icon = clusterIcon;
marker.groundAnchor = CGPointMake(0.5, 0.5);
} else {
if([[marker.userData class] isSubclassOfClass:[POIItem class]]){
POIItem *item = (POIItem *)marker.userData;
MarkerIcon* markerView = (MarkerIcon *)[[NSBundle mainBundle] loadNibNamed:#"MarkerIcon" owner:marker options:nil][0];
marker.iconView = markerView;
marker.groundAnchor = CGPointMake(0.5, 0.5);
}
}
}
It is not a good way to change the code like this. But I could not find better solution for that moment.
I have already sovled this question.
I used googleMaps Api version:8.1.
Here is my code...
#import "Clustering/GMUClusterItem.h"
// Point of Interest Item which implements the GMUClusterItem protocol.
#interface POIItem : NSObject<GMUClusterItem>
#property(nonatomic, readonly) CLLocationCoordinate2D position;
#property(nonatomic, readonly) NSString *name;
- (instancetype)initWithPosition:(CLLocationCoordinate2D)position name:(NSString *)name;
#end
1.creat the map.
#interface BasicViewController ()<GMUClusterManagerDelegate, GMSMapViewDelegate,
GMUClusterRendererDelegate>
#end
typedef NS_ENUM(NSInteger, ClusterAlgorithmMode) {
kClusterAlgorithmGridBased,
kClusterAlgorithmQuadTreeBased,
};
#implementation BasicViewController {
GMSMapView *_mapView;
GMUClusterManager *_clusterManager;
}
- (void)loadView {
//创建地图
GMSCameraPosition *camera =
[GMSCameraPosition cameraWithLatitude:kCameraLatitude longitude:kCameraLongitude zoom:10];
_mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.view = _mapView;
}
2.creat GMUClusterManager object.
- (void)viewDidLoad {
[super viewDidLoad];
//添加标注算法方式
id<GMUClusterAlgorithm> algorithm = [self algorithmForMode:kClusterAlgorithmQuadTreeBased];
//标注icon
id<GMUClusterIconGenerator> iconGenerator = [self iconGeneratorWithImages];//[self defaultIconGenerator];
// CustomClusterIconGenerator *iconGenerator = [[CustomClusterIconGenerator alloc] init];
GMUDefaultClusterRenderer *renderer =
[[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView
clusterIconGenerator:iconGenerator];
renderer.delegate = self;
_clusterManager =
[[GMUClusterManager alloc] initWithMap:_mapView algorithm:algorithm renderer:renderer];
// Generate and add random items to the cluster manager.
//将标注添加到地图上
[self generateClusterItems];
// Call cluster() after items have been added to perform the clustering and rendering on map.
//展示
[_clusterManager cluster];
// Register self to listen to both GMUClusterManagerDelegate and GMSMapViewDelegate events.
[_clusterManager setDelegate:self mapDelegate:self];
UIBarButtonItem *removeButton =
[[UIBarButtonItem alloc] initWithTitle:#"Remove"
style:UIBarButtonItemStylePlain
target:self
action:#selector(removeClusterManager)];
self.navigationItem.rightBarButtonItems = #[ removeButton ];
}
3.in method :
/*You can set marker image here
if [marker class] is POIItem.
*/
- (void)renderer:(id<GMUClusterRenderer>)renderer willRenderMarker:(GMSMarker *)marker {
if ([marker.userData isKindOfClass:[POIItem class]]) {
POIItem *item = marker.userData;
marker.title = item.name;
******marker.icon = [UIImage imageNamed:#"register"];******
}
}
I am trying to dynamically set the pin color for these two points which belong to different classes however they do not show a different color on my map. Do you see any issue in my for loop for my array, towards the bottom?
#define UNIVERSITY_LATITUDE 35.22836;
#define UNIVERSITY_LONGITUDE 126.84784;
#define HOTEL_LATITUDE 34.55;
#define HOTEL_LONGITUDE 127.36;
#implementation MapKitViewController
//Synthesize the getters and setters of the map view
#synthesize mapView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *annotations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
//used to test Annotations of map, will be replaced by for loop to insert new points
PhotoPoint *photoAnn;
photoAnn = [[PhotoPoint alloc] init];
location.latitude =UNIVERSITY_LATITUDE;
location.longitude = UNIVERSITY_LONGITUDE;
photoAnn.coordinate = location;
[photoAnn setTitle:#"GIST"];
[photoAnn setSubtitle:#"University"];
[photoAnn setAnnotationType:#"Photo"];
[annotations addObject:photoAnn];
CommercialPoint *commAnn;
commAnn = [[CommercialPoint alloc] init];
location.latitude =HOTEL_LATITUDE;
location.longitude = HOTEL_LONGITUDE;
commAnn.coordinate = location;
[commAnn setTitle:#"Hotel"];
[commAnn setSubtitle:#"Hotel Center"];
[commAnn setAnnotationType:#"Commercial"];
[annotations addObject:commAnn];
for (id eachObject in annotations){
if ([eachObject isKindOfClass:[PhotoPoint class]])
{
photoAnn.pinColor = MKPinAnnotationColorGreen;
}
if ([eachObject isKindOfClass:[CommercialPoint class]])
{
commAnn.pinColor = MKPinAnnotationColorPurple;
}
//send the delgate messages to the mapview
mapView.delegate = self;
[self.mapView addAnnotations:annotations];
I have a map that zooms appropriately to the current location on load, but then doesn't allow you to pan around the map without it immediately zooming back in on the user location. I have been playing around with tracking mode but haven't gotten the right fix.. Here is some code, I appreciate the help.
- (void)viewDidLoad
{
[super viewDidLoad];
contentArray = [[NSMutableArray alloc] init];
mapViewuno.delegate = self;
mapViewuno.mapType = MKMapTypeStandard;
mapViewuno.userInteractionEnabled=YES;
locationManager = [[CLLocationManager alloc] init];
// Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
indexValue = 0;
NSString* plistPath = [[NSBundle mainBundle] pathForResource:#"mapAddress" ofType:#"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSString *strID = [NSString stringWithFormat:#"%d",intID];
NSLog(#"array : %#",[dict objectForKey:strID]);
[contentArray removeAllObjects];
[contentArray addObjectsFromArray:[dict objectForKey:strID]];
[contentArray retain];
[self zoomToUserLocation:mapViewuno.userLocation];
}
- (void)zoomToUserLocation:(MKUserLocation *)userLocation
{
if (!userLocation)
return;
MKCoordinateRegion region;
region.center = userLocation.location.coordinate;
region.span = MKCoordinateSpanMake(.5, .5);
region = [mapViewuno regionThatFits:region];
[mapViewuno setRegion:region animated:YES];
counter = 0;
[mapViewuno removeAnnotations:mapViewuno.annotations];
if([contentArray count] != 0)
{
for(indexValue = 0; indexValue<[contentArray count];indexValue++)
{
FirstAnnotation *obj=[[FirstAnnotation alloc]init];
obj.latitude = [[[contentArray objectAtIndex:indexValue] objectForKey:#"lattitude"] floatValue];
obj.longitude = [[[contentArray objectAtIndex:indexValue] objectForKey:#"Longitude"] floatValue];
obj.titleName=[[contentArray objectAtIndex:indexValue] objectForKey:#"Title"];
obj.Address = [[contentArray objectAtIndex:indexValue] objectForKey:#"Address"];
obj.Phone = [[contentArray objectAtIndex:indexValue] objectForKey:#"Phone"];
obj.intTag = indexValue;
[mapViewuno addAnnotation:obj];
}
if ([mapViewuno.annotations count] == 0) return;
// [self.mapView setRegion:newRegion animated:YES];
}
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self zoomToUserLocation:userLocation];
}
This part is causing the problem:
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self zoomToUserLocation:userLocation];
}
Every time the mapview's location manager gets a new fix (i.e. every second or so) the mapview calls that delegate method which in turn calls your method to zoom. Put in a BOOL like zoomedToUserLocation and set it to NO at init time, then to YES when it hits zoomToUserLocation the first time.
I have the following files:
annotation.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface annotation : NSObject <MKAnnotation>
#property(nonatomic, assign) CLLocationCoordinate2D coordinate;
#property(nonatomic, copy) NSString *title;
#property(nonatomic, copy) NSString *subtitle;
#end
annotation.m:
#import "annotation.h"
#implementation annotation
#synthesize coordinate, title, subtitle;
#end
And in the main code, which takes in an NSURL found elsewhere:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[spinner stopAnimating];
// json parsing
results = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
annotation * myAnn;
NSArray *pins = mapView.annotations;
if ([pins count])
{
[mapView removeAnnotations:pins];
}
/* loop through the array, each key in the array has a JSON String with format:
* title <- string
* strap <- string
* id <- int
* date <- date
* lat <- floating point double
* long <- floating point double
* link <- string
*/
int i;
for (i = 0; i < [results count]; i++) {
//NSLog(#"Result: %i = %#", i, results[i]);
//NSLog(#"%#",[[results objectAtIndex:i] objectForKey:#"long"]);
myAnn = [[annotation alloc] init];
location.latitude = (double)[[[results objectAtIndex:i] objectForKey:#"lat"] doubleValue];
location.longitude = (double)[[[results objectAtIndex:i] objectForKey:#"long"] doubleValue];
myAnn.coordinate = location;
myAnn.title = [[results objectAtIndex:i] objectForKey:#"title"];
myAnn.subtitle = [[results objectAtIndex:i] objectForKey:#"strap"];
[locations addObject:myAnn];
//NSLog(#"%i", [[results objectAtIndex:i] objectForKey:#"lat"]);
}
[self.mapView addAnnotations:locations];
Previous things I have looked at for this say that I need to use MKAnnotationView as opposed to MKPinAnnotationView but as you can see I do not use either, is it possible for me to use custom images for the pins that are dropped on the screen.
You (a) make sure to define your view controller to be the delegate for your MKMapView; and (b) implement viewForAnnotation, e.g.:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return nil;
}
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
static NSString * const identifier = #"MyCustomAnnotation";
MKAnnotationView* annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView) {
annotationView.annotation = annotation;
} else {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier];
}
// set your annotationView properties
annotationView.image = [UIImage imageNamed:#"Your image here"];
annotationView.canShowCallout = YES;
// if you add QuartzCore to your project, you can set shadows for your image, too
//
// [annotationView.layer setShadowColor:[UIColor blackColor].CGColor];
// [annotationView.layer setShadowOpacity:1.0f];
// [annotationView.layer setShadowRadius:5.0f];
// [annotationView.layer setShadowOffset:CGSizeMake(0, 0)];
// [annotationView setBackgroundColor:[UIColor whiteColor]];
return annotationView;
}
return nil;
}
By the way, in my example above, I changed the name of your annotation class to CustomAnnotation. annotation is a horrible name for a class because (a) it doesn't follow class naming conventions of upper case first letter; and (b) it's identical to the variable name, annotation, that many MKMapViewDelegate methods will use by default.
References
Location Awareness Programming Guide
Map Kit Framework Reference
You can certainly use custom images for an MKAnnotationView, see iOS MapKit custom pins.
Both MKAnnotationPinView and MKAnnotationView are subclasses of UIView and therefore will allow custom views.