I want to show multiple marker on google map. There are answers based on this. But markers are not showing on the map. Although I am getting the latitude and longitude value based on the array result. What should I do?
Note: I have done some changes and the code running perfectly.
My code is:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self performRequestForRestaurantListing];
geometryDict=[[NSMutableDictionary alloc]init];
locationDict=[[NSMutableDictionary alloc]init];
NSLog(#"the value of list is %#", _service);
NSLog(#"the value of stringradius is %#", _stringRadius);
/*---location Manager Initialize-------*/
self.manager=[[CLLocationManager alloc]init];
self.manager.distanceFilter = 100;
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
[self.manager requestAlwaysAuthorization];
self.manager.delegate=self;
[self.manager startUpdatingLocation];
[mapView setDelegate:self];
latitude=#"22.5726";
longitude=#"88.3639";
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[latitude doubleValue]
longitude:[longitude doubleValue]
zoom:12];
[mapView animateToCameraPosition:camera];
[self coordinateOnMap:latitude andWithLongitude:longitude];
}
-(void)coordinateOnMap:(NSString*)latitude andWithLongitude:(NSString*)longitude
{
GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
CLLocationCoordinate2D location;
for (int i=0;i<[restaurantList count];i++)
{
driverMarker = [[GMSMarker alloc] init];
latitude=[[[[restaurantList objectAtIndex:i]objectForKey:#"geometry"]objectForKey:#"location"] objectForKey:#"lat"];
longitude=[[[[restaurantList objectAtIndex:i]objectForKey:#"geometry"]objectForKey:#"location"] objectForKey:#"lng"];
location.latitude = [latitude floatValue];
location.longitude = [longitude floatValue];
driverMarker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
driverMarker.map = mapView;
}
driverMarker.icon=[UIImage imageNamed:#"marker"];
bounds = [bounds includingCoordinate:driverMarker.position];
driverMarker.title = #"My locations";
[driverMarker setTappable:NO];
mapView.myLocationEnabled = YES;
}
I guess your driveMarker gets deallocated by ARC immediatly after each loop.
If this really is your issue, you'll have to make sure that those markers "survive" the loop, e.g. with the following code:
#implementation MyController
#property (nonatomic) NSMutableArray *allMarkers;
- (void)viewDidLoad {
allMarkers = [[NSMutableArray alloc] init];
// ...
}
-(void)coordinateOnMap:(NSString*)latitude andWithLongitude:(NSString*)longitude {
//...
[allMarkers removeAllObjects];
for (int i=0;i<[restaurantList count];i++) {
GMSMarker *driverMarker = [[GMSMarker alloc] init];
[allMarkers addObject:driveMarker];
// ...
}
}
#end
This will create an NSArray property to store all created markers, just to keep them in scope.
I'm trying to populate a detail disclosure but it won't generate. I don't have any errors or warnings when compiling and building the app except one here : "local declaration of mapView hides instance variable. I know what that means, it means that its already used elsewhere. and when i type self.webView the error goes away but the detail disclosure still doesn't populate. let me know what i'm doing wrong please:
Annotations:
MapViewAnnotion.h
#import
Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapViewAnnotation : NSObject <MKAnnotation> {
NSString *title;
NSString *subtitle;
CLLocationCoordinate2D coordinate;
}
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *subtitle;
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sub;
#end
MapViewAnnotation.m
#import "MapViewAnnotation.h"
#implementation MapViewAnnotation
#synthesize title, coordinate,subtitle;
- (id)initWithTitle:(NSString *)ttl andCoordinate:(CLLocationCoordinate2D)c2d andSubtitle:(NSString *)sub{
//[super init];
title = ttl;
coordinate = c2d;
subtitle = sub;
return self;
}
- (void)dealloc {
// [title release];
// [super dealloc];
}
#end
and then...
MapViewController.h
#import <UIKit/UIKit.h>
#import "RESideMenu.h"
#import <MapKit/MapKit.h>
#interface MapViewController : UIViewController
{
IBOutlet MKMapView *mapView;
IBOutlet UISegmentedControl *segmentedControl;
}
#property (nonatomic, retain) MKMapView *mapView;
#property (nonatomic, retain) UISegmentedControl *segmentedControl;
- (IBAction)segmentedControllChanged:(id)sender;
#end
MapViewController.m
#import "MapViewController.h"
#import "MapViewAnnotation.h"
#import "config.h"
#interface MapViewController ()
#end
#implementation MapViewController
#synthesize mapView, segmentedControl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = [NSString stringWithFormat:#"\tCurrently In : %#",[config getSubTitle]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(showMenu)];
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor colorWithRed:(93/255.0) green:(149/255.0) blue:(197/255.0) alpha:1]];
// All map stuff
mapView.showsUserLocation = YES;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[config getTitle] andCoordinate:location andSubtitle:[config getSubTitle]];
[self.mapView addAnnotation:newAnnotation];
// Do any additional setup after loading the view from its nib.
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.99, .99);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
//!!! Map Points
// UNITED STATES
// Alaska
MKPointAnnotation *anchorage = [[MKPointAnnotation alloc] init];
anchorage.coordinate = CLLocationCoordinate2DMake(61.218056, -149.900278);
anchorage.title = #"Anchorage";
anchorage.subtitle = #"Alaska";
[mapView addAnnotation:anchorage];
MKPointAnnotation *chena = [[MKPointAnnotation alloc] init];
chena.coordinate = CLLocationCoordinate2DMake(64.88349, -146.855596);
chena.title = #"Chena Hot Springs";
chena.subtitle = #"Alaska";
[mapView addAnnotation:chena];
MKPointAnnotation *healy = [[MKPointAnnotation alloc] init];
healy.coordinate = CLLocationCoordinate2DMake(63.869738, -149.021511);
healy.title = #"Healy";
healy.subtitle = #"Alaska";
[mapView addAnnotation:healy];
MKPointAnnotation *palmer = [[MKPointAnnotation alloc] init];
palmer.coordinate = CLLocationCoordinate2DMake(61.599722, -149.112778);
palmer.title = #"Palmer";
palmer.subtitle = #"Alaska";
[mapView addAnnotation:palmer];
MKPointAnnotation *seward = [[MKPointAnnotation alloc] init];
seward.coordinate = CLLocationCoordinate2DMake(60.104167, -149.442222);
seward.title = #"Seward";
seward.subtitle = #"Alaska";
[mapView addAnnotation:seward];
MKPointAnnotation *wasilla = [[MKPointAnnotation alloc] init];
wasilla.coordinate = CLLocationCoordinate2DMake(61.581389, -149.439444);
wasilla.title = #"Wasilla";
wasilla.subtitle = #"Alaska";
[mapView addAnnotation:wasilla];
// Arizona
MKPointAnnotation *tucson = [[MKPointAnnotation alloc] init];
tucson.coordinate = CLLocationCoordinate2DMake(32.221743, -110.926479);
tucson.title = #"Tuscon";
tucson.subtitle = #"Arizona";
[mapView addAnnotation:tucson];
MKPointAnnotation *phoenix = [[MKPointAnnotation alloc] init];
phoenix.coordinate = CLLocationCoordinate2DMake(33.448377, -112.074037);
phoenix.title = #"Phoenix";
phoenix.subtitle = #"Arizona";
[mapView addAnnotation:phoenix];
// California
MKPointAnnotation *anaheim = [[MKPointAnnotation alloc] init];
anaheim.coordinate = CLLocationCoordinate2DMake(33.835293, -117.914504);
anaheim.title = #"Anaheim";
anaheim.subtitle = #"California";
[mapView addAnnotation:anaheim];
// Colorado
MKPointAnnotation *denver = [[MKPointAnnotation alloc] init];
denver.coordinate = CLLocationCoordinate2DMake(39.737567, -104.984718);
denver.title = #"Denver";
denver.subtitle = #"Colorado";
[mapView addAnnotation:denver];
// Georgia
MKPointAnnotation *augusta = [[MKPointAnnotation alloc] init];
augusta.coordinate = CLLocationCoordinate2DMake(33.473498, -82.010515);
augusta.title = #"Augusta";
augusta.subtitle = #"Georgia";
[mapView addAnnotation:augusta];
// Hawaii
MKPointAnnotation *honolulu = [[MKPointAnnotation alloc] init];
honolulu.coordinate = CLLocationCoordinate2DMake(21.306944, -157.858333);
honolulu.title = #"Honolulu";
honolulu.subtitle = #"Hawaii";
[mapView addAnnotation:honolulu];
// Indiana
MKPointAnnotation *terrehaute = [[MKPointAnnotation alloc] init];
terrehaute.coordinate = CLLocationCoordinate2DMake(39.466703, -87.413909);
terrehaute.title = #"Terre Haute";
terrehaute.subtitle = #"Indiana";
[mapView addAnnotation:terrehaute];
MKPointAnnotation *indianapolis = [[MKPointAnnotation alloc] init];
indianapolis.coordinate = CLLocationCoordinate2DMake(39.768403, -86.158068);
indianapolis.title = #"Indianapolis";
indianapolis.subtitle = #"Indiana";
[mapView addAnnotation:indianapolis];
// Michigan
MKPointAnnotation *westland = [[MKPointAnnotation alloc] init];
westland.coordinate = CLLocationCoordinate2DMake(42.324204, -83.400211);
westland.title = #"Westland";
westland.subtitle = #"Michigan";
[mapView addAnnotation:westland];
MKPointAnnotation *detroit = [[MKPointAnnotation alloc] init];
detroit.coordinate = CLLocationCoordinate2DMake(42.331427, -83.045754);
detroit.title = #"Detroit";
detroit.subtitle = #"Michigan";
[mapView addAnnotation:detroit];
// Missouri
MKPointAnnotation *stlouis = [[MKPointAnnotation alloc] init];
stlouis.coordinate = CLLocationCoordinate2DMake(38.627003, -90.199404);
stlouis.title = #"St Louis";
stlouis.subtitle = #"Missouri";
[mapView addAnnotation:stlouis];
// New York
MKPointAnnotation *newyork = [[MKPointAnnotation alloc] init];
newyork.coordinate = CLLocationCoordinate2DMake(40.712784, -74.005941 );
newyork.title = #"New York";
newyork.subtitle = #"New York";
[mapView addAnnotation:newyork];
MKPointAnnotation *manhattan = [[MKPointAnnotation alloc] init];
manhattan.coordinate = CLLocationCoordinate2DMake(40.790278, -73.959722);
manhattan.title = #"Manhattan";
manhattan.subtitle = #"New York";
[mapView addAnnotation:manhattan];
MKPointAnnotation *queens = [[MKPointAnnotation alloc] init];
queens.coordinate = CLLocationCoordinate2DMake(40.75, -73.866667);
queens.title = #"Queens";
queens.subtitle = #"New York";
[mapView addAnnotation:queens];
// North Carolina
MKPointAnnotation *asheville = [[MKPointAnnotation alloc] init];
asheville.coordinate = CLLocationCoordinate2DMake(35.595058, -82.551487);
asheville.title = #"Asheville";
asheville.subtitle = #"North Carolina";
[mapView addAnnotation:asheville];
MKPointAnnotation *butner = [[MKPointAnnotation alloc] init];
butner.coordinate = CLLocationCoordinate2DMake(36.132089, -78.756671);
butner.title = #"Butner";
butner.subtitle = #"North Carolina";
[mapView addAnnotation:butner];
MKPointAnnotation *creedmoor = [[MKPointAnnotation alloc] init];
creedmoor.coordinate = CLLocationCoordinate2DMake(36.122368, -78.686115);
creedmoor.title = #"Creedmoor";
creedmoor.subtitle = #"North Carolina";
[mapView addAnnotation:creedmoor];
MKPointAnnotation *charlotte = [[MKPointAnnotation alloc] init];
charlotte.coordinate = CLLocationCoordinate2DMake(35.227087, -80.843127);
charlotte.title = #"Charolette";
charlotte.subtitle = #"North Carolina";
[mapView addAnnotation:charlotte];
MKPointAnnotation *fayetteville = [[MKPointAnnotation alloc] init];
fayetteville.coordinate = CLLocationCoordinate2DMake(35.052664, -78.878358);
fayetteville.title = #"Fayetteville";
fayetteville.subtitle = #"North Carolina";
[mapView addAnnotation:fayetteville];
MKPointAnnotation *jacksonville = [[MKPointAnnotation alloc] init];
jacksonville.coordinate = CLLocationCoordinate2DMake(34.754052, -77.430241);
jacksonville.title = #"Jacksonville";
jacksonville.subtitle = #"North Carolina";
[mapView addAnnotation:jacksonville];
MKPointAnnotation *greenville = [[MKPointAnnotation alloc] init];
greenville.coordinate = CLLocationCoordinate2DMake(35.612661, -77.366354);
greenville.title = #"Greenville";
greenville.subtitle = #"North Carolina";
[mapView addAnnotation:greenville];
MKPointAnnotation *greensboro = [[MKPointAnnotation alloc] init];
greensboro.coordinate = CLLocationCoordinate2DMake(36.072635, -79.791975);
greensboro.title = #"Greensboro";
greensboro.subtitle = #"North Carolina";
[mapView addAnnotation:greensboro];
MKPointAnnotation *winston = [[MKPointAnnotation alloc] init];
winston.coordinate = CLLocationCoordinate2DMake(36.09986, -80.244216);
winston.title = #"Winston";
winston.subtitle = #"North Carolina";
[mapView addAnnotation:winston];
MKPointAnnotation *boone = [[MKPointAnnotation alloc] init];
boone.coordinate = CLLocationCoordinate2DMake(36.216795, -81.674552);
boone.title = #"Boone";
boone.subtitle = #"North Carolina";
[mapView addAnnotation:boone];
MKPointAnnotation *raleigh = [[MKPointAnnotation alloc] init];
raleigh.coordinate = CLLocationCoordinate2DMake(35.77959, -78.638179);
raleigh.title = #"Raleigh";
raleigh.subtitle = #"North Carolina";
[mapView addAnnotation:raleigh];
MKPointAnnotation *durham = [[MKPointAnnotation alloc] init];
durham.coordinate = CLLocationCoordinate2DMake(35.994033, -78.898619);
durham.title = #"Durham";
durham.subtitle = #"North Carolina";
[mapView addAnnotation:durham];
MKPointAnnotation *goldsboro = [[MKPointAnnotation alloc] init];
goldsboro.coordinate = CLLocationCoordinate2DMake(35.384884, -77.992765);
goldsboro.title = #"Goldsboro";
goldsboro.subtitle = #"North Carolina";
[mapView addAnnotation:goldsboro];
MKPointAnnotation *kinston = [[MKPointAnnotation alloc] init];
kinston.coordinate = CLLocationCoordinate2DMake(35.262664, -77.581635);
kinston.title = #"Kinston";
kinston.subtitle = #"North Carolina";
[mapView addAnnotation:kinston];
MKPointAnnotation *lagrange = [[MKPointAnnotation alloc] init];
lagrange.coordinate = CLLocationCoordinate2DMake(35.306829, -77.788034);
lagrange.title = #"La Grange";
lagrange.subtitle = #"North Carolina";
[mapView addAnnotation:lagrange];
MKPointAnnotation *oxford = [[MKPointAnnotation alloc] init];
oxford.coordinate = CLLocationCoordinate2DMake(36.3107, -78.590835);
oxford.title = #"Oxford";
oxford.subtitle = #"North Carolina";
[mapView addAnnotation:oxford];
MKPointAnnotation *morrisville = [[MKPointAnnotation alloc] init];
morrisville.coordinate = CLLocationCoordinate2DMake(35.823483, -78.825562);
morrisville.title = #"Morrisville";
morrisville.subtitle = #"North Carolina";
[mapView addAnnotation:morrisville];
MKPointAnnotation *roxboro = [[MKPointAnnotation alloc] init];
roxboro.coordinate = CLLocationCoordinate2DMake(36.393752, -78.982788);
roxboro.title = #"Roxboro";
roxboro.subtitle = #"North Carolina";
[mapView addAnnotation:roxboro];
MKPointAnnotation *henderson = [[MKPointAnnotation alloc] init];
henderson.coordinate = CLLocationCoordinate2DMake(36.329591, -78.399164);
henderson.title = #"Henderson";
henderson.subtitle = #"North Carolina";
[mapView addAnnotation:henderson];
MKPointAnnotation *chapelhill = [[MKPointAnnotation alloc] init];
chapelhill.coordinate = CLLocationCoordinate2DMake(35.9132, -79.055845);
chapelhill.title = #"Chapel Hill";
chapelhill.subtitle = #"North Carolina";
[mapView addAnnotation:chapelhill];
MKPointAnnotation *apex = [[MKPointAnnotation alloc] init];
apex.coordinate = CLLocationCoordinate2DMake(35.732652, -78.850286);
apex.title = #"Apez";
apex.subtitle = #"North Carolina";
[mapView addAnnotation:apex];
// Oklahoma
MKPointAnnotation *lawton = [[MKPointAnnotation alloc] init];
lawton.coordinate = CLLocationCoordinate2DMake(34.603567, -98.395929);
lawton.title = #"Lawton";
lawton.subtitle = #"Oklahoma";
[mapView addAnnotation:lawton];
// Pennsylvania
MKPointAnnotation *mercersburg = [[MKPointAnnotation alloc] init];
mercersburg.coordinate = CLLocationCoordinate2DMake(39.82787, -77.903331);
mercersburg.title = #"Mercersburg";
mercersburg.subtitle = #"Pennsylvania";
[mapView addAnnotation:mercersburg];
// South Carolina
MKPointAnnotation *columbia = [[MKPointAnnotation alloc] init];
columbia.coordinate = CLLocationCoordinate2DMake(34.00071, -81.034814);
columbia.title = #"Columbia";
columbia.subtitle = #"South Carolina";
[mapView addAnnotation:columbia];
MKPointAnnotation *greenville2 = [[MKPointAnnotation alloc] init];
greenville2.coordinate = CLLocationCoordinate2DMake(34.852618, -82.39401);
greenville2.title = #"Greenville";
greenville2.subtitle = #"South Carolina";
[mapView addAnnotation:greenville2];
MKPointAnnotation *mbeach = [[MKPointAnnotation alloc] init];
mbeach.coordinate = CLLocationCoordinate2DMake(33.68906, -78.886694);
mbeach.title = #"Mrytle Beach";
mbeach.subtitle = #"South Carolina";
[mapView addAnnotation:mbeach];
// Tennessee
MKPointAnnotation *chattanooga = [[MKPointAnnotation alloc] init];
chattanooga.coordinate = CLLocationCoordinate2DMake(35.04563, -85.30968);
chattanooga.title = #"Chattanooga";
chattanooga.subtitle = #"Tennessee";
[mapView addAnnotation:chattanooga];
MKPointAnnotation *manchester = [[MKPointAnnotation alloc] init];
manchester.coordinate = CLLocationCoordinate2DMake(35.481743, -86.088599);
manchester.title = #"Manchester";
manchester.subtitle = #"Tennessee";
[mapView addAnnotation:manchester];
MKPointAnnotation *nashville = [[MKPointAnnotation alloc] init];
nashville.coordinate = CLLocationCoordinate2DMake(36.166667, -86.783333);
nashville.title = #"Nashville";
nashville.subtitle = #"Tennessee";
[mapView addAnnotation:nashville];
MKPointAnnotation *knoxville = [[MKPointAnnotation alloc] init];
knoxville.coordinate = CLLocationCoordinate2DMake(35.960638, -83.920739);
knoxville.title = #"Knoxville";
knoxville.subtitle = #"Tennessee";
[mapView addAnnotation:knoxville];
// Washington D.C.
MKPointAnnotation *wdc = [[MKPointAnnotation alloc] init];
wdc.coordinate = CLLocationCoordinate2DMake(38.907192, -77.036871);
wdc.title = #"Washington D.C.";
[mapView addAnnotation:wdc];
// Virgina
MKPointAnnotation *alexandria = [[MKPointAnnotation alloc] init];
alexandria.coordinate = CLLocationCoordinate2DMake(38.804836, -77.046921);
alexandria.title = #"Alexandria";
alexandria.subtitle = #"Virgina";
[mapView addAnnotation:alexandria];
MKPointAnnotation *arlington = [[MKPointAnnotation alloc] init];
arlington.coordinate = CLLocationCoordinate2DMake(38.87997, -77.10677);
arlington.title = #"Arlington";
arlington.subtitle = #"Virgina";
[mapView addAnnotation:arlington];
MKPointAnnotation *fairfax = [[MKPointAnnotation alloc] init];
fairfax.coordinate = CLLocationCoordinate2DMake(38.846224, -77.306373);
fairfax.title = #"Fairfax";
fairfax.subtitle = #"Virgina";
[mapView addAnnotation:fairfax];
MKPointAnnotation *williamsburg = [[MKPointAnnotation alloc] init];
williamsburg.coordinate = CLLocationCoordinate2DMake(37.270702, -76.707457);
williamsburg.title = #"Williamsburg";
williamsburg.subtitle = #"Virgina";
[mapView addAnnotation:williamsburg];
// OCONUS
// ASIA
// SOUTH KOREA
MKPointAnnotation *seoul = [[MKPointAnnotation alloc] init];
seoul.coordinate = CLLocationCoordinate2DMake(37.566535, 126.977969);
seoul.title = #"Seoul";
seoul.subtitle = #"South Korea";
[mapView addAnnotation:seoul];
MKPointAnnotation *suwon = [[MKPointAnnotation alloc] init];
suwon.coordinate = CLLocationCoordinate2DMake(37.263573, 127.028601);
suwon.title = #"Suwon";
suwon.subtitle = #"South Korea";
[mapView addAnnotation:suwon];
MKPointAnnotation *incheon = [[MKPointAnnotation alloc] init];
incheon.coordinate = CLLocationCoordinate2DMake(37.456256, 126.705206);
incheon.title = #"Incheon";
incheon.subtitle = #"South Korea";
[mapView addAnnotation:incheon];
MKPointAnnotation *sokcho = [[MKPointAnnotation alloc] init];
sokcho.coordinate = CLLocationCoordinate2DMake(38.207015, 128.591849);
sokcho.title = #"Sokcho-si";
sokcho.subtitle = #"South Korea";
[mapView addAnnotation:sokcho];
MKPointAnnotation *osan = [[MKPointAnnotation alloc] init];
osan.coordinate = CLLocationCoordinate2DMake(37.14981, 127.077221);
osan.title = #"Osan";
osan.subtitle = #"South Korea";
[mapView addAnnotation:osan];
MKPointAnnotation *pyeongtaek = [[MKPointAnnotation alloc] init];
pyeongtaek.coordinate = CLLocationCoordinate2DMake(36.992108, 127.112945);
pyeongtaek.title = #"Pyeongtaek-si";
pyeongtaek.subtitle = #"South Korea";
[mapView addAnnotation:pyeongtaek];
// THAILAND
MKPointAnnotation *bangkok = [[MKPointAnnotation alloc] init];
bangkok.coordinate = CLLocationCoordinate2DMake(13.727896, 100.524123);
bangkok.title = #"Bangkok";
bangkok.subtitle = #"Thailand";
[mapView addAnnotation:bangkok];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;
} else {
pinView.annotation = annotation;
}
return pinView;
}
- (void)showMenu
{
[self.sideMenuViewController presentMenuViewController];
}
- (IBAction)segmentedControllChanged:(id)sender {
if (segmentedControl.selectedSegmentIndex == 0) {
mapView.mapType = MKMapTypeStandard;
}else if (segmentedControl.selectedSegmentIndex == 1) {
mapView.mapType = MKMapTypeHybrid;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.199, .199);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
} else if (segmentedControl.selectedSegmentIndex == 2) {
mapView.mapType = MKMapTypeSatellite;
double cost2 = [config getLatitude];
double cost = [config getLongitude];
CLLocationCoordinate2D location;
location.latitude = cost2;
location.longitude = cost;
MKCoordinateRegion region;
region.center.latitude = (double) cost2;
region.center.longitude = (double) cost;
region.span = MKCoordinateSpanMake(.299, .299);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
any help is welcome :) Thank you
I've created a route from A to B with MKMapItem, but I can't change the title of the annotation on the map. My code:
CLLocation *locationRestaurante = [[CLLocation alloc] initWithLatitude:[[[listaRestaurante objectAtIndex:0] objectForKey:#"latitude"] floatValue] longitude:[[[listaRestaurante objectAtIndex:0] objectForKey:#"longitude"] floatValue]];
MKPlacemark *place = [[MKPlacemark alloc] initWithCoordinate:locationRestaurante.coordinate addressDictionary:nil];
MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
NSDictionary *options = #{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving};
[mapItem openInMapsWithLaunchOptions:options];
it opens everything just fine... But there's only one problem: The I can't change this annotation name:
mapItem.name = #"Place name";
MKMapItem Class Reference
I have just started to use MapKit framework, and my first practice did not go well :)
Everything seems to be works fine except MapView showing somewhere in ocean and it did not find any place (ex: Land, island). I think latitude and longitude getting out of range somehow.
here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *zoomButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-In"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomIn:)];
UIBarButtonItem *typeButton =
[[UIBarButtonItem alloc]
initWithTitle: #"Zoom-Out"
style:UIBarButtonItemStyleBordered
target: self
action:#selector(zoomOut:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:zoomButton, typeButton, nil];
barButtons.items = buttons;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"My Company";
annotationPoint.subtitle = #"my Company Title";
//[self __mapView];
MKCoordinateRegion adjustedRegion = [__mapView regionThatFits:viewRegion];
[__mapView setRegion:adjustedRegion animated:YES];
[__mapView addAnnotation:annotationPoint];
}
You are setting zoomLocation coordinates after creating region. Try first to set zoomLocation
...
zoomLocation.latitude = 48.431638;
zoomLocation.longitude= 27.169436;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
...
So I have the following code block, which is supposed to iterate over an array of JSON objects and place MKPointAnnotations on a map:
for(id jsonObject in dataArray)
{
NSLog(#"%d",[dataArray count]);
NSDictionary* jsonDictionary = jsonObject;
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
NSString *lat = [jsonDictionary objectForKey:#"latitude"];
NSString *lon = [jsonDictionary objectForKey:#"longitude"];
point.coordinate.latitude = [lat doubleValue];
point.coordinate.longitude = [lon doubleValue];
[map addAnnotation:point];
}
However, the two lines:
point.coordinate.latitude = [lat doubleValue];
point.coordinate.longitude = [lon doubleValue];
are giving me an "Expression is not Assignable" error. I can't for the life of me figure it out. I've tried to make a CLLocationCoordinate2D object and assigning that, but it doesn't work either.
This should work:
CLLocationCoordinate2d coordinate = ...
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coordinate;
[mapView addAnnotation:annotation];
It works in an existing app, just checked the code and the app.
Check this answer as well: https://stackoverflow.com/a/15162092/1032151