我的项目只能在模拟器中运行,但不能在设备上运行,为什么? [英] My project run only in simulator but do not work in device, why?
问题描述
我在地图上创建了一条折线。并在地图视图中成功创建多段线。
我的问题是,这段代码只运行模拟器。不要在设备上工作。我尝试了很多次,但没有在设备上工作。
生成IPA并安装到我的设备中,并崩溃我的应用程序。为什么?并简单地在模拟器中运行,为什么?
为什么这样开心?请帮助
Thankyou
我的代码
- ( void)centerMapForCoordinateArray:(CLLocationCoordinate2D *)routes andCount:(int)count {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx< count; idx ++)
{
CLLocationCoordinate2D currentLocation = routes [idx];
if(currentLocation.latitude> maxLat)
maxLat = currentLocation.latitude;
if(currentLocation.latitude minLat = currentLocation.latitude;
if(currentLocation.longitude> maxLon)
maxLon = currentLocation.longitude;
if(currentLocation.longitude< minLon)
minLon = currentLocation.longitude;
}
region.center.latitude =(maxLat + minLat)/ 2;
region.center.longitude =(maxLon + minLon)/ 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
[self.mapView setRegion:region animated:YES];
$ b $(MKPolyline *)polylineWithEncodedString :( NSString *)encodedString {
const char * bytes = [encodedString UTF8String];
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger idx = 0;
NSUInteger count = length / 4;
CLLocationCoordinate2D * coords = calloc(count,sizeof(CLLocationCoordinate2D));
NSUInteger coordIdx = 0;
float latitude = 0;
float longitude = 0;
while(idx< length){
char byte = 0;
int res = 0;
char shift = 0;
do {
byte = bytes [idx ++] - 63;
res | =(byte& 0x1F)<<转移;
shift + = 5;
} while(byte> = 0x20); (res& 1)?〜(res>> 1):(res>> 1));
float deltaLat =
latitude + = deltaLat;
shift = 0;
res = 0;
do {
byte = bytes [idx ++] - 0x3F;
res | =(byte& 0x1F)<<转移;
shift + = 5;
} while(byte> = 0x20); (res& 1)?〜(res>> 1):(res>> 1));
float deltaLon =
longitude + = deltaLon;
float finalLat =纬度* 1E-5;
float finalLon = longitude * 1E-5;
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat,finalLon);
coords [coordIdx ++] = coord;
if(coordIdx == count){
NSUInteger newCount = count + 10;
coords = realloc(coords,newCount * sizeof(CLLocationCoordinate2D));
count = newCount;
}
}
MKPolyline * polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
free(coords);
返回折线;
$ b - (void)getDirections {
//越野
//37.705553,-122.372074至25.883937,-80.223026
//在家工作
//37.7577,-122.4376至37.764473,-122.399639
CLLocation * newLocation = [[CLLocation alloc] initWithLatitude:22.7001469经度: 75.8758194];
MKPointAnnotation * annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude,newLocation.coordinate.longitude);
annotation.title = @你;
[self.mapView addAnnotation:annotation];
CLLocation * keyPlace = [[CLLocation alloc] initWithLatitude:22.6924483 longitude:75.8653895];
MKPointAnnotation * endannotation = [[MKPointAnnotation alloc] init];
endannotation.coordinate = CLLocationCoordinate2DMake(keyPlace.coordinate.latitude,keyPlace.coordinate.longitude);
endannotation.title = @学校;
[self.mapView addAnnotation:endannotation];
CLLocationCoordinate2D endCoordinate;
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination= %f,%f& sensor = false& mode = driving,newLocation.coordinate.latitude,newLocation.coordinate.longitude,keyPlace.coordinate.latitude,keyPlace.coordinate.longitude]];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:& response error:& error];
$ b if(!error){
NSDictionary * responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:& error];
NSLog(@response ==%@,responseDict);
if([[responseDict valueForKey:@status] isEqualToString:@ZERO_RESULTS]){
[[[UIAlertView alloc] initWithTitle:@Error
message:@Could不从您当前的位置路由路径
委托:无
cancelButtonTitle:@Close
otherButtonTitles:nil,nil] show];
return;
}
int points_count = 0;
if([[responseDict objectForKey:@routes] count])
points_count = [[[[[[responseDict objectForKey:@routes] objectAtIndex:0] objectForKey:@legs] objectAtIndex:0] objectForKey:@steps] count];
$ b $ if(!points_count){
[[[UIAlertView alloc] initWithTitle:@Error
message:@无法从当前位置路由路径
委托:无
cancelButtonTitle:@Close
otherButtonTitles:nil,nil] show];
return;
}
CLLocationCoordinate2D points [points_count];
NSLog(@routes%@,[[[[responseDict objectForKey:@routes] objectAtIndex:0] objectForKey:@overview_polyline] objectForKey:@points]
);
MKPolyline * polyline = [self polylineWithEncodedString:[[[[ResponseDict objectForKey:@routes] objectAtIndex:0] objectForKey:@overview_polyline] objectForKey:@points]];
[self.mapView addOverlay:polyline];
int j = 0;
NSArray * steps = nil;
if(points_count&& [[[[responseDict objectForKey:@routes] objectAtIndex:0] objectForKey:@legs] count])
steps = [[[[[ :@routes] objectAtIndex:0] objectForKey:@legs] objectAtIndex:0] objectForKey:@steps];
for(int i = 0; i< points_count; i ++){
double st_lat = [[[[steps objectAtIndex:i] objectForKey:@start_location] valueForKey:@ lat] doubleValue];
double st_lon = [[[[steps [objectAtIndex:i] objectForKey:@start_location] valueForKey:@lng] doubleValue];
// NSLog(@lat lon:%f%f,st_lat,st_lon);
if(st_lat> 0.0f& st_lon> 0.0f){
points [j] = CLLocationCoordinate2DMake(st_lat,st_lon);
j ++;
}
double end_lat = [[[[steps [objectAtIndex:i] objectForKey:@end_location] valueForKey:@lat] doubleValue];
double end_lon = [[[[steps [objectAtIndex:i] objectForKey:@end_location] valueForKey:@lng] doubleValue];
// NSLog(@lat%f lng%f,end_lat,end_lon);
// if(end_lat> 0.0f&& end_lon> 0.0f){
points [j] = CLLocationCoordinate2DMake(end_lat,end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat,end_lon);
j ++;
//
}
NSLog(@points Count%d,points_count);
// MKPolyline * polyline = [MKPolyline polylineWithCoordinates:points count:points_count];
// [self.mapView addOverlay:polyline];
[self centerMapForCoordinateArray:points andCount:points_count]; (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id< MKAnnotation>)注解{b
$ b}
#pragma mark - MapKit
-
MKPinAnnotationView * annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@currentloc];
annView.canShowCallout = YES;
annView.animatesDrop = YES;
返回annView;
$ b - (MKOverlayView *)mapView:(MKMapView *)mapView
viewForOverlay:(id< MKOverlay>)overlay {
MKPolylineView * overlayView = [[MKPolylineView alloc] initWithOverlay:overlay];
overlayView.lineWidth = 5;
overlayView.strokeColor = [UIColor purpleColor];
overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5f];
返回overlayView;
尝试在iphone4s中运行或5模拟器,如果它在模拟器崩溃比变化
points [j] = CLLocationCoordinate2DMake(end_lat,end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat,end_lon);
j ++;
随着
< pre $ if(j <= points_count){
points [j] = CLLocationCoordinate2DMake(end_lat,end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat,end_lon);
j ++;
}
为什么?因为
我们声明 CLLocationCoordinate2D points [points_count];
So max Coordinate2D
是 points_count
但某些纬度经度 j
值高于 points_count
因此,如果条件为 j
请检查并让我知道它解决了您的问题或不。
I create a polyline in map. and successfully create polyline in map view. My problem is, This code run only simulator. do not work on device. I tried to many times but do not work on device.
Genrate the IPA and install in my device and crash my app. why? and simply run in simulator, why?
Why is this happining? please help Thankyou
my code
-(void) centerMapForCoordinateArray:(CLLocationCoordinate2D *)routes andCount:(int)count{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx <count; idx++)
{
CLLocationCoordinate2D currentLocation = routes[idx];
if(currentLocation.latitude > maxLat)
maxLat = currentLocation.latitude;
if(currentLocation.latitude < minLat)
minLat = currentLocation.latitude;
if(currentLocation.longitude > maxLon)
maxLon = currentLocation.longitude;
if(currentLocation.longitude < minLon)
minLon = currentLocation.longitude;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
[self.mapView setRegion:region animated:YES];
}
- (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString {
const char *bytes = [encodedString UTF8String];
NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSUInteger idx = 0;
NSUInteger count = length / 4;
CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
NSUInteger coordIdx = 0;
float latitude = 0;
float longitude = 0;
while (idx < length) {
char byte = 0;
int res = 0;
char shift = 0;
do {
byte = bytes[idx++] - 63;
res |= (byte & 0x1F) << shift;
shift += 5;
} while (byte >= 0x20);
float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
latitude += deltaLat;
shift = 0;
res = 0;
do {
byte = bytes[idx++] - 0x3F;
res |= (byte & 0x1F) << shift;
shift += 5;
} while (byte >= 0x20);
float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
longitude += deltaLon;
float finalLat = latitude * 1E-5;
float finalLon = longitude * 1E-5;
CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
coords[coordIdx++] = coord;
if (coordIdx == count) {
NSUInteger newCount = count + 10;
coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
count = newCount;
}
}
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];
free(coords);
return polyline;
}
- (void)getDirections {
//Cross country
//37.705553,-122.372074 to 25.883937,-80.223026
//Home to work
//37.7577,-122.4376 to 37.764473,-122.399639
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:22.7001469 longitude:75.8758194];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
annotation.title = @"You";
[self.mapView addAnnotation:annotation];
CLLocation *keyPlace = [[CLLocation alloc] initWithLatitude:22.6924483 longitude:75.8653895];
MKPointAnnotation *endannotation = [[MKPointAnnotation alloc] init];
endannotation.coordinate = CLLocationCoordinate2DMake(keyPlace.coordinate.latitude, keyPlace.coordinate.longitude);
endannotation.title = @"School";
[self.mapView addAnnotation:endannotation];
CLLocationCoordinate2D endCoordinate;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=driving", newLocation.coordinate.latitude, newLocation.coordinate.longitude, keyPlace.coordinate.latitude, keyPlace.coordinate.longitude]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];
NSLog(@"response == %@",responseDict);
if ([[responseDict valueForKey:@"status"] isEqualToString:@"ZERO_RESULTS"]) {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not route path from your current location"
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil, nil] show];
return;
}
int points_count = 0;
if ([[responseDict objectForKey:@"routes"] count])
points_count = [[[[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"] count];
if (!points_count) {
[[[UIAlertView alloc] initWithTitle:@"Error"
message:@"Could not route path from your current location"
delegate:nil
cancelButtonTitle:@"Close"
otherButtonTitles:nil, nil] show];
return;
}
CLLocationCoordinate2D points[points_count];
NSLog(@"routes %@", [[[[responseDict objectForKey:@"routes"] objectAtIndex:0]objectForKey:@"overview_polyline"] objectForKey:@"points"]
);
MKPolyline *polyline = [self polylineWithEncodedString:[[[[responseDict objectForKey:@"routes"] objectAtIndex:0]objectForKey:@"overview_polyline"] objectForKey:@"points"]];
[self.mapView addOverlay:polyline];
int j = 0;
NSArray *steps = nil;
if (points_count && [[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] count])
steps = [[[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
for (int i = 0; i < points_count; i++) {
double st_lat = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lat"] doubleValue];
double st_lon = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lng"] doubleValue];
//NSLog(@"lat lon: %f %f", st_lat, st_lon);
if (st_lat > 0.0f && st_lon > 0.0f) {
points[j] = CLLocationCoordinate2DMake(st_lat, st_lon);
j++;
}
double end_lat = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lat"] doubleValue];
double end_lon = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lng"] doubleValue];
//NSLog(@"lat %f lng %f",end_lat,end_lon);
//if (end_lat > 0.0f && end_lon > 0.0f) {
points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
j++;
//}
}
NSLog(@"points Count %d",points_count);
// MKPolyline *polyline = [MKPolyline polylineWithCoordinates:points count:points_count];
// [self.mapView addOverlay:polyline];
[self centerMapForCoordinateArray:points andCount:points_count];
}
}
#pragma mark - MapKit
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
annView.canShowCallout = YES;
annView.animatesDrop = YES;
return annView;
}
- (MKOverlayView *)mapView:(MKMapView *)mapView
viewForOverlay:(id<MKOverlay>)overlay {
MKPolylineView *overlayView = [[MKPolylineView alloc] initWithOverlay:overlay];
overlayView.lineWidth = 5;
overlayView.strokeColor = [UIColor purpleColor];
overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.5f];
return overlayView;
}
Try to Run in iphone4s or 5 Simulator, if it crash in simulator than change
points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
j++;
With
if (j<=points_count) {
points[j] = CLLocationCoordinate2DMake(end_lat, end_lon);
endCoordinate = CLLocationCoordinate2DMake(end_lat, end_lon);
j++;
}
Why? Because
we declare CLLocationCoordinate2D points[points_count];
So max Coordinate2D
is points_count
but some latitude longitude j
value is higher than the points_count
so we have to put if condition for j
Check it and let me know it it solve your problem or not.
这篇关于我的项目只能在模拟器中运行,但不能在设备上运行,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!