This question already has answers here:
iPhone: Create MKAnnotation
(3 answers)
Closed 8 years ago.
How to set marker in MKMapView
Here is my code to set a latitude and longitude but i dont know how to set marker
please give me solution.
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
MKCoordinateRegion region;
MKCoordinateSpan span;
location.latitude = center.latitude; //37.250556;
location.longitude = center.longitude; //-96.358333;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
[mapView regionThatFits:region];
Useful tutorial: maybelost.com/2011/01/a-basic-mapview-and-annotation-tutorial
// Add the annotation to our map view
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:#"Buckingham Palace" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];
[newAnnotation release];
Related
I have create a zone on the map and save this coordinate and current zoom level in the database. When we edit this zone and open the map in the mapkit with the same coordinate and zoomlevel region on the map shown is different what we have created.
CLLocationCoordinate2D cd= CLLocationCoordinate2DMake(30.724300, 76.723166);
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(cd, 4640, 4565);
[self.mapView setRegion:viewRegion animated:YES];
Using this function I get the region width and height
-(void) getMapDelta:(CLLocationCoordinate2D) coord
{
MKCoordinateSpan span = self.mapView.region.span;
CLLocationCoordinate2D loc = self.mapView.region.center;
//get latitude in meters
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude - span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:(self.mapView.region.center.latitude + span.latitudeDelta * 0.5) longitude:self.mapView.region.center.longitude];
//get longitude in meters
CLLocation *loc3 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude - span.longitudeDelta * 0.5)];
CLLocation *loc4 = [[CLLocation alloc] initWithLatitude:self.mapView.region.center.latitude longitude:(self.mapView.region.center.longitude + span.longitudeDelta * 0.5)];
int metersLatitude = [loc1 distanceFromLocation:loc2];
int metersLongitude = [loc3 distanceFromLocation:loc4];
NSLog(#"Delta> %d / %d", metersLongitude,metersLatitude);
NSLog(#"Coor> %f / %f", coord.latitude,coord.longitude);
}
I am using google map SDKs in my iOS project to track the user location and a draw a route line of the user path for his movement.I want to add two images for the starting point of the user and another for user movement. I can't add these images.Please help me to add these images. Here is my code:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//get the latest location
CLLocation *currentLocation = [locations lastObject];
//store latest location in stored track array;
[self.locations addObject:currentLocation];
//get latest location coordinates
CLLocationDegrees Latitude = currentLocation.coordinate.latitude;
CLLocationDegrees Longitude = currentLocation.coordinate.longitude;
CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude);
//zoom map to show users location
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 1000,1000);
MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion];
[mapview setRegion:adjustedRegion animated:YES];
NSInteger numberOfSteps = self.locations.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [self.locations objectAtIndex:index];
CLLocationCoordinate2D coordinate2 = location.coordinate;
coordinates[index] = coordinate2;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[mapview addOverlay:polyLine];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 10.0;
return polylineView;
}
This code draw the user path. But I want this type of output.
Someone please help me.
Add annotations to mapview, for first and last objects of your self.locations array. You will also need to remove the old annotation each time the location is updated to a new one.
Let centerY and centerX be the center point for the image
CLLocationDegrees offset = 0.01; // change size here
CLLocationDegrees southWestY = centerY - offset;
CLLocationDegrees southWestX = centerX - offset;
CLLocationDegrees northEastY = centerY + offset;
CLLocationDegrees northEastX = centerX + offset;
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestY,southWestX);
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastY,northEastX);
GMSCoordinateBounds *overlayBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:southWest coordinate:northEast];
GMSGroundOverlay *overlay = [GMSGroundOverlay groundOverlayWithBounds:overlayBounds icon:[UIImage imagedNamed:#"YourImage.png"]];
overlay.bearing = 0;
overlay.map = self.mapView
With this approach (unlike with the marker) the image will zoom in and out as the user zooms in/out of the map.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm making this app where people can see what tourist attractions are near them and I'm a bit stuck!
I need it to load the user's current location when the app is loaded and zoom to it, however it's not doing so.
Sorry to be a bit blunt but I really don't know how to set it up so that it will work with both the button and load their location automatically.
https://pbs.twimg.com/media/BcRSTk0CAAEV47Q.jpg
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#interface ViewController : UIViewController{
MKMapView *mapview;
}
#property (weak, nonatomic) IBOutlet MKMapView *myMapView;
-(IBAction)getlocation;
#end
ViewController.m
#import "ViewController.h"
#import "Annotation.h"
#interface ViewController ()
#end
//Thorpe Park Coordinates
#define THORPE_LATITUDE 51.40395;
#define THORPE_LONGITUDE -0.51433;
//London Eye Coordinates
#define LONDONEYE_LATITUDE 51.50340;
#define LONDONEYE_LONGITUDE -0.11952;
//The New London Dungeons
#define LONDONDUGNEONS_LATITUDE 51.50231;
#define LONDONDUGNEONS_LONGITUDE -0.11965;
//Span
#define THE_SPAN 0.50f;
#implementation ViewController
#synthesize myMapView;
-(IBAction)getlocation {
mapview.showsUserLocation = YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude = LONDONEYE_LATITUDE;
center.longitude = LONDONEYE_LONGITUDE;
//Span
MKCoordinateSpan span;
span.latitudeDelta = THE_SPAN;
span.longitudeDelta = THE_SPAN;
myRegion.center = center;
myRegion.span = span;
//Set our mapView
[myMapView setRegion:myRegion animated:YES];
//Annotation's
NSMutableArray * locations = [ [NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation *myAnn;
//London Eye
myAnn = [[Annotation alloc] init];
location.latitude = LONDONEYE_LATITUDE;
location.longitude = LONDONEYE_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"The London Eye";
myAnn.subtitle = #"";
[locations addObject:myAnn];
//The New London Dungeons
myAnn = [[Annotation alloc] init];
location.latitude = LONDONDUGNEONS_LATITUDE;
location.longitude = LONDONDUGNEONS_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"The London Dungeons";
myAnn.subtitle = #"";
[locations addObject:myAnn];
//Thorpe Park
myAnn = [[Annotation alloc] init];
location.latitude = THORPE_LATITUDE;
location.longitude = THORPE_LONGITUDE;
myAnn.coordinate = location;
myAnn.title = #"Thorpe Park";
myAnn.subtitle = #"";
[locations addObject:myAnn];
[self.myMapView addAnnotations:locations];
/*
//1. Create a coordinate to be used with pin
CLLocationCoordinate2D ThorpeLocation;
ThorpeLocation.latitude = THORPE_LATITUDE;
ThorpeLocation.longitude = THORPE_LONGITUDE;
Annotation * myAnnotation = [Annotation alloc];
myAnnotation.coordinate = ThorpeLocation;
myAnnotation.title = #"Thorpe Park";
myAnnotation.subtitle = #"Theme Park";
[self.myMapView addAnnotation:myAnnotation];
*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Use this, and don't forget to put the delegate on your .h file, hope it works ;)
_mapView.userTrackingMode = YES;
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.00001;
span.longitudeDelta = 0.00001;
CLLocationCoordinate2D location = _mapView.userLocation.coordinate;
region.span = span;
region.center = location;
[_mapView setRegion:region animated:TRUE];
[_mapView regionThatFits:region];
}
I am adding annotations and there are supposed to be 5 annotation pins on my map but only 2 appear and this error is shown:
An instance 0x9d8e720 of class Annotation was deallocated while key
value observers were still registered with it. Observation info was
leaked, and may even become mistakenly attached to some other object.
Set a breakpoint on NSKVODeallocateBreak to stop h
//NUH COORDINATES
#define NUH_latitude 1.293700;
#define NUH_longitude 103.783353;
//span
#define the_span 0.002f;
//NHGP COORDINATES!
#define NHGP_latitude 1.295938;
#define NHGP_longitude 103.782857;
//NUHER COORDINATES
#define NUHER_latitude 1.294968;
#define NUHER_longitude 103.783716;
//NUCIS COORDINATES
#define NUCIS_latitude 1.293149;
#define NUCIS_longitude 103.783464;
//Viva-UCCC COORDINATES!
#define VivaUCCC_latitude 1.294099;
#define VivaUCCC_longitude 103.783233;
//span
MKCoordinateSpan span;
span.latitudeDelta = the_span;
span.longitudeDelta = the_span;
MKCoordinateRegion myRegion;
//center
CLLocationCoordinate2D center;
center.latitude = NUH_latitude;
center.longitude = NUH_longitude;
myRegion.center =center;
myRegion.span = span;
[mapview setRegion:myRegion animated:YES];
//annotation
NSMutableArray * locations = [[NSMutableArray alloc] init];
CLLocationCoordinate2D location;
Annotation * myAnn;
//NUH location
myAnn = [[Annotation alloc] init];
location.latitude = NUH_longitude;
location.longitude = NUH_longitude;
myAnn.coordinate = location;
myAnn.title = #"NUH";
[locations addObject:myAnn];
//NHGP location
myAnn = [[Annotation alloc] init];
location.latitude = NHGP_latitude;
location.longitude = NHGP_longitude;
myAnn.coordinate = location;
myAnn.title = #"National Healthcare Group Polyclinic ";
[locations addObject:myAnn];
//NUHER location
myAnn = [[Annotation alloc] init];
location.latitude = NUHER_longitude;
location.longitude = NUHER_longitude;
myAnn.coordinate = location;
myAnn.title = #"National University Hospital Emergency Room";
[locations addObject:myAnn];
//NUCIS location
myAnn = [[Annotation alloc] init];
location.latitude = NUCIS_longitude;
location.longitude = NUCIS_longitude;
myAnn.coordinate = location;
myAnn.title = #"National University Cancer Institute Singapore";
[locations addObject:myAnn];
//Viva-UCCC location
myAnn = [[Annotation alloc] init];
location.latitude = VivaUCCC_latitude;
location.longitude = VivaUCCC_longitude;
myAnn.coordinate = location;
myAnn.title = #"Viva-University Children's Cancer Centre";
[locations addObject:myAnn];
[self.mapview addAnnotations:locations];
You've specified longitudes for the latitudes for NUH, NUHER, and NUCIS. They won't show up where you intended (which is why you only see 2), and I think the error is because those values are not valid for latitudes.
//NUH location
location.latitude = NUH_longitude; // <-- should be NUH_latitude
...
//NUHER location
location.latitude = NUHER_longitude; // <-- should be NUHER_latitude
...
//NUCIS location
location.latitude = NUCIS_longitude; // <-- should be NUCIS_latitude
It could be because you keep reallocting the same myAnn variable. You should try creating multiple Annotations, call them myAnn1, myAnn2 if you have to.
Later on if you have more than 3 of these things you should consider making a collection and looping through it rather than creating them one-by-one
I've been adding MapView into my ViewController. Code as below:
MapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
MKCoordinateRegion region;
region.center.latitude = [[myStore.Location objectAtIndex:0] doubleValue];
region.center.longitude = [[myStore.Location objectAtIndex:1] doubleValue];
MKCoordinateSpan span;
span.latitudeDelta = .0015;
span.longitudeDelta = .0015;
region.span = span;
[MapView setRegion:region animated:YES];
myStore.Location is an array with coordinates. XCode just crashes the app and return the error at [MapView setRegion:region animated:YES]; which i assume that it cannot init the mapview, can anyone help?
i've resolved my problem... the latitude and longitude was being reversed. [myStore.Location objectAtIndex:0] should be the longitude and [myStore.Location objectAtIndex:1] should be the latitude... silly me.