I am trying to add pins from the server and show it one the map.
Normally I can show the pin but I want to add it online. I have three parsed json data NAME, Longitude and Latitude. I have parsed it in array. I couldn't know how to view it on the map
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = 40.0000;
annotationCoord.longitude = 29.000;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = name;
[self.locationMap addAnnotation:annotationPoint];
I have tried to add annotationCoord.latitude and annotationCoord.longitude in for loop but I get this error "bad receiver type 'CLLocationDegrees' (akadouble)" I think I am making big mistake but where I couldn't know. Please need help.
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
for (int i = 0; i < jsonArray.count; i++) {
NSString *lat = [jsonArray objectAtIndex:i];
[annotationCoord.latitude addObject:lat];
}
}
My JSON return:
response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://kalkatawi.com/mapLocation.php"]];
NSError *parseError = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
jsonArray1 = [[NSMutableArray alloc] init];
jsonArray2 = [[NSMutableArray alloc] init];
jsonArray3 = [[NSMutableArray alloc] init];
for(int i=0;i<[jsonArray count];i++)
{
name = [[jsonArray objectAtIndex:i] objectForKey:#"name"];
[jsonArray1 addObject:name];
}
for(int i=0;i<[jsonArray count];i++)
{
longitude = [[jsonArray objectAtIndex:i] objectForKey:#"longitude"];
[jsonArray2 addObject:longitude];
}
for(int i=0;i<[jsonArray count];i++)
{
latitude = [[jsonArray objectAtIndex:i] objectForKey:#"latitude"];
[jsonArray3 addObject:latitude];
}
self.locationMap.delegate = self;
Based on the updated code, jsonArray already is an array of dictionaries with each dictionary holding the properties of each annotation.
I don't understand why you want to split that up into three separate arrays (one for each property).
Why not use jsonArray as-is to create the annotations:
jsonArray = [NSJSONSerialization JSONObjectWithData:...
self.locationMap.delegate = self; //set delegate before adding annotations
for (int i=0; i < [jsonArray count]; i++)
{
NSDictionary *annotationDictionary = [jsonArray objectAtIndex:i];
name = [annotationDictionary objectForKey:#"name"];
annotationCoord.latitude
= [[annotationDictionary objectForKey:#"latitude"] doubleValue];
annotationCoord.longitude
= [[annotationDictionary objectForKey:#"longitude"] doubleValue];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = name;
[self.locationMap addAnnotation:annotationPoint];
}
The updated code in the question also shows it looping through jsonArray in the didUpdateUserLocation delegate method. It's not clear why this is being done in that delegate method but if you're planning to update the annotations on the map every time the user moves, you may also need to remove all/some existing annotations before adding again to avoid duplicate annotations.
Try
- (void)addAnnotations:(NSArray *)annotations;
of MKMApView
The explanation given in top answer. I only convert this code into Swift 3.
Swift 3
jsonArray = JSONSerialization.jsonObjectWithData()
locationMap.delegate = self
for i in 0..<jsonArray.count() {
var annotationDictionary = jsonArray[i]
name = annotationDictionary["name"]
annotationCoord.latitude = annotationDictionary["latitude"]!
annotationCoord.longitude = annotationDictionary["longitude"]!
var annotationPoint = MKPointAnnotation()
annotationPoint.coordinate = annotationCoord
annotationPoint.title = name
locationMap.addAnnotation(annotationPoint)
}
Related
I am trying to remove existing all markers from google maps, we can do by map.clear but I don't want to remove everything(Polyline, polygons) on map, I just want to remove only markers
I am creating markers based on array count
-(void)annotationCreationForCoordinatesOfArray:(NSMutableArray *)array
{
for (int i=0; i<array.count; i++)
{
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[[array objectAtIndex:i] objectForKey:#"latitude"] doubleValue], [[[array objectAtIndex:i] objectForKey:#"longitude"] doubleValue]);
mark = [GMSMarker markerWithPosition:position];
NSString *annoNumber = [NSString stringWithFormat:#"%i",i];
mark.title = annoNumber;
mark.map = _mapView;
mark.tracksViewChanges = YES;
mark.draggable = YES;
mark.icon = [UIImage imageNamed:#"Mappin.png"];
}
}
Try this code its work for me.
Create global array
NSMutableArray *removalMarkerArray;
Now add all marker in global array
removalMarkerArray=[[NSMutableArray alloc]init];
-(void)annotationCreationForCoordinatesOfArray:(NSMutableArray *)array{
for (int i=0; i<array.count; i++){
CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[[array objectAtIndex:i] objectForKey:#"latitude"] doubleValue], [[[array objectAtIndex:i] objectForKey:#"longitude"] doubleValue]);
mark = [GMSMarker markerWithPosition:position];
NSString *annoNumber = [NSString stringWithFormat:#"%i",i];
mark.title = annoNumber;
mark.map = _mapView;
mark.tracksViewChanges = YES;
mark.draggable = YES;
mark.icon = [UIImage imageNamed:#"Mappin.png"];
[removalMarkerArray addObject:mark];
}
}
Then where you want to clear all marker
for (GMSMarker *marker in removalMarkerArray ){
marker.map = nil;
}
I am inserting some annotations that are coming from a json server, but I wanted to check if the annotation is already on the map, if so, does not add it again. For they are being added on each other , have someone help me solve this problem?
my code:
// adiciona produtos ao mapa
- (void)adicionaAnnotationsNoMapa:(id)objetos{
NSMutableArray *annotationsPins = [[NSMutableArray alloc] init];
for (NSDictionary *annotationDeProdutos in objetos) {
CLLocationCoordinate2D location;
AnnotationMap *myAnn;
myAnn = [[AnnotationMap alloc] init];
location.latitude = [[annotationDeProdutos objectForKey:#"latitude"] floatValue];
location.longitude = [[annotationDeProdutos objectForKey:#"longitude"] floatValue];
myAnn.coordinate = location;
myAnn.title = [annotationDeProdutos objectForKey:#"name"];
myAnn.subtitle = [NSString stringWithFormat:#"R$ %#",[annotationDeProdutos objectForKey:#"price"]];
myAnn.categoria = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id_categoria"]];
myAnn.idProduto = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id"]];
[annotationsPins addObject:myAnn];
}
[self.mapView addAnnotations:annotationsPins];
}
You can iterate through annotations already on MkMapView and see if they are already there:
NSArray * annotations = [self.mapView.annotations copy];
for (NSDictionary *annotationDeProdutos in objetos)
{
// Check if annotation in annotations are duplicate of annotationDeProdutos.
// You can match using name.
}
Or the other way if you only want to show annotations from single server call:
[self.mapView removeAnnotations: self.mapView.annotations];
// Now add from JSON response.
You can do this for each of your annotations:
if(![self.mapView.annotations containsObject: myAnn]) {
[self.mapView addAnnotations: myAnn];
}
I solved the problem with this code :
// adiciona produtos ao mapa
- (void)adicionaAnnotationsNoMapa:(id)objetos{
NSMutableArray *annotationsPins = [[NSMutableArray alloc] init];
for (NSDictionary *annotationDeProdutos in objetos) {
CLLocationCoordinate2D location;
AnnotationMap *myAnn;
myAnn = [[AnnotationMap alloc] init];
location.latitude = [[annotationDeProdutos objectForKey:#"latitude"] floatValue];
location.longitude = [[annotationDeProdutos objectForKey:#"longitude"] floatValue];
myAnn.coordinate = location;
myAnn.title = [annotationDeProdutos objectForKey:#"name"];
myAnn.subtitle = [NSString stringWithFormat:#"R$ %#",[annotationDeProdutos objectForKey:#"price"]];
myAnn.categoria = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id_categoria"]];
myAnn.idProduto = [NSString stringWithFormat:#"%#", [annotationDeProdutos objectForKey:#"id"]];
if (self.mapView.annotations.count <1) {
[annotationsPins addObject:myAnn];
} else {
__block NSInteger foundIndex = NSNotFound;
[annotationsPins enumerateObjectsUsingBlock:^(AnnotationMap *annotation, NSUInteger idx, BOOL *stop) {
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:location.latitude longitude:location.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:annotation.coordinate.latitude longitude:annotation.coordinate.longitude];
if ([loc1 distanceFromLocation:loc2] <= 1.0f) {
foundIndex = idx;
*stop = YES;
}
}];
if (foundIndex != NSNotFound) {
[annotationsPins addObject:myAnn];
}
}
}
[self.mapView addAnnotations:annotationsPins];
}
First of all I am running a Map page that just only show pins on map for every store. I was running only one pin on the map and it was fast after I put more than 25 pins it push very slow to the map page. What it is doing now at that process the app just load all data of the pin location (as I see in the target output) and then it push to the next screen. So please where would be my problem?
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"";
self.navigationItem.title = #"Mek";
response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://kalkatawi.com/mapLocation.php"]];
if(response!=nil) {
NSError *parseError = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
jsonArray1 = [[NSMutableArray alloc] init];
jsonArray2 = [[NSMutableArray alloc] init];
jsonArray3 = [[NSMutableArray alloc] init];
for(int i=0;i<[jsonArray count];i++)
{
name = [[jsonArray objectAtIndex:i] objectForKey:#"name"];
longitude = [[jsonArray objectAtIndex:i] objectForKey:#"longitude"];
latitude = [[jsonArray objectAtIndex:i] objectForKey:#"latitude"];
[jsonArray1 addObject:name];
[jsonArray2 addObject:longitude];
[jsonArray3 addObject:latitude];
self.locationMap.delegate = self; //set delegate before adding annotations
CLLocationCoordinate2D annotationCoord;
for (int i=0; i < [jsonArray count]; i++)
{
NSDictionary *annotationDictionary = [jsonArray objectAtIndex:i];
name = [annotationDictionary objectForKey:#"name"];
annotationCoord.latitude
= [[annotationDictionary objectForKey:#"longitude"] doubleValue];
annotationCoord.longitude
= [[annotationDictionary objectForKey:#"latitude"] doubleValue];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = name;
annotationPoint.subtitle = [NSString stringWithFormat:#"%f %f", annotationPoint.coordinate.latitude, annotationPoint.coordinate.longitude];
[self.locationMap addAnnotation:annotationPoint];
//------------------------//
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(annotationPoint.coordinate, 50000, 50000);
[self.locationMap setRegion:[self.locationMap regionThatFits:region] animated:YES];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
lat = locationManager.location.coordinate.latitude;
lon = locationManager.location.coordinate.longitude;
}
}
}
else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"You are not connected to internet" message:#"Please check the internet connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *pinView = nil;
if(annotation != locationMap.userLocation)
{
static NSString *defaultPinID = #"myPin";
pinView = (MKAnnotationView *)[locationMap dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.image = [UIImage imageNamed:#"pinpinpin.png"];
pinView.canShowCallout = YES;
pinView.enabled = YES;
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = infoButton;
}
return pinView;
}
SImple way is load data in background thread, and then when data is fully load display it on the map. You can do this in any of view controller, means you can do it in parent view controller and when you got response then update on map view controller.
Or on view did load method load data in background and update it when its load. This approach will not hold your UI. You can use blocks like this
dispatch_async(queue, ^{
//load data from server
dispatch_async(dispatch_get_main_queue(), ^{
//Update map
});
});
Now I loop through one Array at once and calculate distance like this:
- (void)calculateDistance
{
ann = [dict objectForKey:#"Blue"];
for(int i = 0; i < [ann count]; i++) {
NSString *coordinates = [[ann objectAtIndex:i] objectForKey:#"Coordinates"];
double realLatitude = [[[coordinates componentsSeparatedByString:#","] objectAtIndex:1] doubleValue];
double realLongitude = [[[coordinates componentsSeparatedByString:#","] objectAtIndex:0] doubleValue];
// Calculating distance
CLLocation *pinLocation = [[CLLocation alloc]
initWithLatitude:realLatitude
longitude:realLongitude];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:mapView.userLocation.coordinate.latitude
longitude:mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
// Adding distance to dictionaries
if (distance > 1000) {
NSString *dist = [NSString stringWithFormat:#"%.2f km.", distance/1000];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:#"Distance"];
}
else{
NSString *dist = [NSString stringWithFormat:#"%4.0f m.", distance];
NSMutableDictionary *inDict = [[NSMutableDictionary alloc] init];
inDict = [ann objectAtIndex:i];
[inDict setValue:dist forKey:#"Distance"];
}
}
}
My data structure is:
How to loop through all Array's at once? I have Array which contains all my Array's named "resultArray", but this code doesn't work:
ann = [dict objectForKey:resultArray];
NSLog(#"%#", resultArray);
2013-05-05 10:57:03.643 testApp[5708:907] (
Black,
Green,
Orange,
Blue,
Darkblue
)
I guess you want to enumerate through the keys stored in resultArray and calculate the distance and add that calculated values to it.
- (void)calculateDistance
{
//Enumerates through resultArray
for (NSString *key in resultArray) {
//ann array is considered as an instance of NSMutableArray
ann = dict[key];
for(int i = 0; i < [ann count]; i++) {
NSMutableDictionary *inDict = [ann[i] mutableCopy];
NSString *coordinates = inDict[#"Coordinates"];
NSArray *coordinateComponents = [coordinates componentsSeparatedByString:#","];
double realLatitude = [coordinateComponents[1] doubleValue];
double realLongitude = [coordinateComponents[0] doubleValue];
// Calculating distance
CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:realLatitude
longitude:realLongitude];
CLLocation *userLocation = [[CLLocation alloc]
initWithLatitude:mapView.userLocation.coordinate.latitude
longitude:mapView.userLocation.coordinate.longitude];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];
// Adding distance to dictionaries
if (distance > 1000) {
NSString *dist = [NSString stringWithFormat:#"%.2f km.", distance/1000];
[inDict setValue:dist forKey:#"Distance"];
}
else{
NSString *dist = [NSString stringWithFormat:#"%4.0f m.", distance];
[inDict setValue:dist forKey:#"Distance"];
}
//Inserting the modified values to the main array
[ann replaceObjectAtIndex:i withObject:inDict];
}
}
}
ann = [dict objectForKey:resultArray];
for(NSArray *colourArray in ann)
{
for(NSDictionary *itemDictionary in colourArray)
{
NSLog(#"Coordinates = %#",[itemDictionary objectForKey:#"Coordinates"]);
NSLog(#"Name = %#",[itemDictionary objectForKey:#"Name"]);
NSLog(#"Address = %#",[itemDictionary objectForKey:#"Address"]);
}
}
Hope it will help you .
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