Adding MultiplePointAnnotation In MapView ios - ios

Im trying to load annotation point into mapview in which i have the seperate array for latitude and a seperate array for longitude here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
delegate=[[UIApplication sharedApplication]delegate];
delegate.arrForLat=[[NSMutablearray alloc]initwithobjects:#"33.930216",#"33.939788",#"33.9272",#"33.902237"];
delegate.arrForLon=[[NSMutablearray alloc]initwithobjects:#"-118.050392",#"-118.076549",#"-118.065817",#"-118.081733‌​"];
for (int i=0 ; i< delegate.arrForLat.count;i++)
{
annotationCoord.latitude = [[delegate.arrForLat objectAtIndex:i] doubleValue];
annotationCoord.longitude = [[delegate.arrForLng objectAtIndex:i] doubleValue];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
[MapView addAnnotation:annotationPoint];
}
}
Now i get only 1 annotation point in mapview but i have 4 coordinates.I don't know what mistake i have done.

Ah, I think I see it. You only have one annotationCoord. On the first trip through the loop you set its lat and long to (33.930216,-118.050392) and create a new object called annotationPoint point its coordinate attribute to your annotationCoord. Then on the next time through the loop you edit annotationCoord by giving it new coordinates. But it's still the same annotationCoord and the annotation you added to the map is still using it, now with the new coordinates.
So the solution is to make a new CLLocationCoordinate2D each time through the loop.

Related

Dragging doesn't work in Google Maps iOS app?

I have the following method defined in a view controller class. At the last line of this code I declare the draggable attribute to be set to YES, but surprisingly it doesn't work when in the app.
What may cause this?
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
CLLocationDegrees latitude = -33.861;
CLLocationDegrees longitude = 151.20;
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(latitude,longitude);
marker.map = _mapView;
marker.draggable = YES;
}
You need to long push the marker then you can drag it.

MKPolyline only draw points instead of lines

I am trying to track user's route and drawing lines of the route, but the addOverlay only gives me correct points but no connection between each point.
-(void)viewWillAppear:(BOOL)animated{
self.trackPointArray = [[NSMutableArray alloc] init];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(CLLocation *)userLocation
{
[self.trackPointArray addObject:userLocation];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1000, 1000);
[self.myMapView setRegion:[self.myMapView regionThatFits:region] animated:YES];
NSInteger stepsNumber = self.trackPointArray.count;
CLLocationCoordinate2D coordinates[stepsNumber];
for (NSInteger index = 0; index < stepsNumber; index++) {
CLLocation *location = [self.trackPointArray objectAtIndex:index];
coordinates[index] = [location coordinate];
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:stepsNumber];
[self.myMapView addOverlay:polyLine];
}
- (MKOverlayRenderer *)mapView:(MKMapView *)myMapView rendererForOverlay:(id<MKOverlay>)overlay
{
MKPolylineRenderer *polylineRenderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
polylineRenderer.lineWidth = 4.0f;
polylineRenderer.strokeColor = [UIColor redColor];
return polylineRenderer;
}
The userLocation object the map view passes to the didUpdateUserLocation delegate method is the same object every time.
The coordinate inside the object may be different at each moment but each call to the delegate method always points to the same container object.
Specifically, it always points to the same object that the map view's userLocation property points to (mapView.userLocation). You can see this if you NSLog userLocation and mapView.userLocation and notice their memory addresses are the same each time.
For this reason, when the code does this:
[self.trackPointArray addObject:userLocation];
it just adds the same object reference to the array multiple times.
Later, when the code loops through the trackPointArray array, each call to [location coordinate] returns the same coordinate every time because location always points to the same object (mapView.userLocation) and the coordinate does not change during the loop.
So each time the delegate method is called, a polyline is created with N coordinates (all the same) which ends up drawing a "dot".
The reason you see multiple dots is because the code is not removing previous overlays.
To fix all this, one easy way is to create a new CLLocation instance each time you want to add the updated coordinates:
CLLocation *tpLocation = [[CLLocation alloc]
initWithLatitude:userLocation.coordinate.latitude
longitude:userLocation.coordinate.longitude];
[self.trackPointArray addObject:tpLocation];
Additionally, you should remove the previous overlay before adding the updated line. You won't notice the previous lines if you don't do this but they'll be there using up memory and performance:
[self.myMapView removeOverlays:self.myMapView.overlays];
[self.myMapView addOverlay:polyLine];

location on mapView

i have a map and i want to set the user location ( blue dot ) when i open it.i tried lot of things and didn't work.please help me
MKCoordinateRegion region;
region.center.latitude=BG_LATITUDE;
region.center.longitude=BG_LONGITUDE;
region.span.latitudeDelta=SPAN_VALUE;
region.span.longitudeDelta=SPAN_VALUE;
[self.mapView setRegion:region animated:YES];
NSMutableArray *annotations =[[NSMutableArray alloc]init];
//coordinate (for the annotation)
CLLocationCoordinate2D location;
mapAnnotation *ann;
location.latitude=MaB_LATITUDE;
location.longitude=MaB_LONGITUDE;
//annotation
ann = [[mapAnnotation alloc]init];
[ann setCoordinate:location];
ann.title=#"Main Branch";
[annotations addObject:ann];
you dont need to write so much lines of code to show the blue dot , MKMapView itself contains a property called "showUserLocation" which has to be set as true in order to show blue dot on to the Map .
self.mapView.showsUserLocation = TRUE;
It also automatically tracks the Device's Location . Hope it will help you .
Use this if you want show user location on map
self.mapView.showsUserLocation = YES;

Reload annotations in a map view

I am adding annotations as hardcoded values into the method
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
CLLocationCoordinate2D coords1;
coords1.latitude = 40.579754;
coords1.longitude = -120.1303229;
MKPointAnnotation *annotationPoint1 = [[MKPointAnnotation alloc] init];
annotationPoint1.coordinate = coords1;
annotationPoint1.title = #"TJ11234";
annotationPoint1.subtitle = #"Power Failure \n Start Time:12hrs 30min \n End Time:14hrs ";
[self.mapView addAnnotation:annotationPoint1];
with values of lats and longitude hardcode intially its loading fine, but when I go to another page and come back to the page the annotations are not loading. What should I do to correct it?
mapView:didUpdateUserLocation: is called whenever a new location update is received by the map view. Check the documentation.
If you want to set annotation when your map is shown, you can place your code in viewDidAppear or similar.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]
CLLocationCoordinate2D coords1;
coords1.latitude = 40.579754;
coords1.longitude = -120.1303229;
MKPointAnnotation *annotationPoint1 = [[MKPointAnnotation alloc] init];
annotationPoint1.coordinate = coords1;
annotationPoint1.title = #"TJ11234";
annotationPoint1.subtitle = #"Power Failure \n Start Time:12hrs 30min \n End Time:14hrs ";
[self.mapView addAnnotation:annotationPoint1];
}
Hope this helps.

setCoordinate updating after annotation added on my mapView

Im adding a bunch of annotations, and sotirng them in an array so i can pull in their new locations and update the map accordingly WITHOUT removing all the annotations first and adding them which causes a flicker. Unfortunately, after their coordinates are initially set and added any setCoordinate call no longer works. Any ideas?
- (void)updateMap:(NSData *)responseData {
//parse out the JSON data
NSError* error;
vehicleData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
//find which Dictionary is for our bus.
for (NSDictionary* route in vehicleData) {
//find our bus in our vehicles dictionary. Key is by routeID.
BusPositionDot *busLocationDot;
if ([vehicles objectForKey:[route objectForKey:#"RouteID"]] == nil) {
busLocationDot = [[BusPositionDot alloc] init];
[vehicles setObject:busLocationDot forKey:[route objectForKey:#"RouteID"]];
[mapView addAnnotation:[vehicles objectForKey:[route objectForKey:#"RouteID"]]];
}
else {
busLocationDot = [vehicles objectForKey:#"RouteID"];
}
float latitude = [[route objectForKey:#"Latitude"] floatValue];
float longitude = [[route objectForKey:#"Longitude"] floatValue];
float groundSpeed = [[route objectForKey:#"GroundSpeed"] floatValue];
float direction = [[route objectForKey:#"Heading"] floatValue];
float roundedDirection=45 * round(direction/45);
if(groundSpeed<=3)
//get view for annotation
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot.png"];
else if((roundedDirection==0)||(roundedDirection==360))
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot0.png"];
else if(roundedDirection==45)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot45.png"];
else if(roundedDirection==90)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot90.png"];
else if(roundedDirection==135)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot135.png"];
else if(roundedDirection==180)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot180.png"];
else if(roundedDirection==225)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot225.png"];
else if(roundedDirection==270)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot270.png"];
else if(roundedDirection==315)
[mapView viewForAnnotation:busLocationDot].image=[UIImage imageNamed:#"buspositiondot315.png"];
CLLocationCoordinate2D currentBusLocation = CLLocationCoordinate2DMake(latitude, longitude);
NSLog(#"setting coord %f & %f",currentBusLocation.latitude,currentBusLocation.longitude);
[UIView animateWithDuration:0.5 animations:^{
[busLocationDot setCoordinate:currentBusLocation];
}];
}
}
In reference to this part:
if ([vehicles objectForKey:[route objectForKey:#"RouteID"]] == nil) {
...
[mapView addAnnotation:
[vehicles objectForKey:[route objectForKey:#"RouteID"]]];
}
else {
busLocationDot = [vehicles objectForKey:#"RouteID"];
}
when you call addAnnotation, you pass:
[vehicles objectForKey:[route objectForKey:#"RouteID"]]
but if it already exists, you use:
[vehicles objectForKey:#"RouteID"]
This means when updating an annotation, a different (most likely wrong) reference is being used.
Either [vehicles objectForKey:#"RouteID"] is not actually a BusPositionDot or it's not the same instance that was originally added with that "route id".
Therefore, the setCoordinate wouldn't work.
Using the same reference when updating the annotation should fix it.
There are a couple of other unrelated, potential issues:
The code is doing a direct comparison using a float variable (eg. if(roundedDirection==45)). This is not recommended even if it "seems to work" -- there's the potential for floating-point precision errors. Either check if roundedDirection is within a very small range of the target value or, in your case, just declare roundedDirection as an int since it looks like the expression
45 * round(direction/45) will only return values with no fractions.
The code is setting the image of the annotation view directly. This is ok but make sure the viewForAnnnotation delegate method also has the same exact logic to set the image based on direction otherwise what may happen is the annotation view's image will get reset to some default when panning or zooming. You may need to add a direction property to BusPositionDot.

Resources