我在顯示澳大利亞大陸時啓動Map應用程序。我有一個按鈕,我想爲當前位置設置編碼,並在按下按鈕後放大到該位置。我獲得當前緯度&經度並且能夠通過Web服務URL。我下面的搜索按鈕編碼點擊事件當前位置查詢
- (void)viewDidLoad
{
//to set the australia region
CLLocationCoordinate2D AusLoc = {-19.048230,133.685730};
MKCoordinateSpan AusSpan = MKCoordinateSpanMake(45, 45);
MKCoordinateRegion AusRegion = MKCoordinateRegionMake(AusLoc, AusSpan);
mapView.region = AusRegion;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation]; }
和
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
NSLog(@"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
if (currentLocation != nil) {
[currentLocation release];
}
currentLocation = newLocation;
[currentLocation retain];
}
NSString *lati=[NSString stringWithFormat:@"%f", newLocation.coordinate.latitude];
NSString *longi=[NSString stringWithFormat:@"%f", newLocation.coordinate.longitude];
}
(Web服務調用)
NSString *url = [NSString stringWithFormat:@"http://.....url...../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
與編碼更新時間:
- (IBAction) showAddress {
if (locationManager.location == nil)
{
NSLog(@"user location not found yet or service disabled/denied");
}
else
{
// Change map region using span (degrees)...
MKCoordinateSpan span = MKCoordinateSpanMake(0.001, 0.001);
MKCoordinateRegion region = MKCoordinateRegionMake
(locationManager.location.coordinate, span);
[mapView setRegion:region animated:YES];
}
}
當我設置的MapView。 showUserLocation = YES;它會在點擊按鈕之前顯示地圖應用程序啓動時的當前位置。我很困惑以前的答案。因此,我應該在按鈕單擊事件下放置哪些代碼以顯示當前位置,並且還建議在爲Click事件提供編碼之後,是否有任何更改將當前的Lati和longi傳遞給Web服務url。在此先感謝
編輯: -
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@""];
annView.pinColor = MKPinAnnotationColorGreen;
//annView.animatesDrop=TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
annView.enabled = YES;
annView.image = [UIImage imageNamed:@"flagg.png"];
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
annView.rightCalloutAccessoryView = rightButton;
NSLog(@"Created annotation at: %f %f", ((CustomPlacemark*)annotation).coordinate.latitude, ((CustomPlacemark*)annotation).coordinate.longitude);
[annView addObserver:self
forKeyPath:@"selected"
options:NSKeyValueObservingOptionNew
context:@"GMAP_ANNOTATION_SELECTED"];
[annView autorelease];
return annView;
}
//return nil;
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{
NSString *action = (NSString*)context;
// We only want to zoom to location when the annotation is actaully selected. This will trigger also for when it's deselected
if([[change valueForKey:@"new"] intValue] == 1 && [action isEqualToString:@"GMAP_ANNOTATION_SELECTED"])
{
if([((MKAnnotationView*) object).annotation isKindOfClass:[CustomPlacemark class]])
{
CustomPlacemark *place = ((MKAnnotationView*) object).annotation;
// Zoom into the location
[mapView setRegion:place.coordinateRegion animated:TRUE];
NSLog(@"annotation selected: %f %f", ((MKAnnotationView*) object).annotation.coordinate.latitude, ((MKAnnotationView*) object).annotation.coordinate.longitude);
}
}
}
在搜索按鈕(調用Web服務)
CLLocationCoordinate2D location;
//currentLocation = newLocation;
float radius = [[arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
//NSURL *url = [[NSURL alloc] initWithString:@"http://www.hettich.com.au/hespdirectory/phpsqlsearch_genxml.php?lat=-33.853468&lng=150.94042160000004&radius=5"];
NSString *url = [NSString stringWithFormat:@"http://www.hettich.com.au/hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
//NSLog(@"NSString *url");
NSLog(@"%@", url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
{ resultButton.userInteractionEnabled = YES;
// NSMutableArray* annotations = [[NSMutableArray alloc] init];
for (int i = 0; i < [appDelegate.markers count]; i++)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [aMarker.lat floatValue];
location.longitude =[aMarker.lng floatValue];
AddressAnnotation *annob = [[AddressAnnotation alloc] initWithCoordinate:location];
annob.title = aMarker.name;
annob.subTitle = aMarker.address;
[mapView addAnnotation:annob];
[annob release];
CLLocationCoordinate2D ausLoc = {location.latitude,location.longitude}; //for zoom in the showroom results region
MKCoordinateSpan ausSpan = MKCoordinateSpanMake(0.108889, 0.169922);
MKCoordinateRegion ausRegion = MKCoordinateRegionMake(ausLoc, ausSpan);
NSLog(@"No Errors");
mapView.region = ausRegion;
}
}
只有在輕按按鈕或用戶位置發生變化時,您是否希望將地圖放在用戶位置上? – Anna
當我打開應用程序時,我有澳大利亞大陸的mapView和覆蓋屏幕一半的一個UIview。 UIview包含以下按鈕Show Me,Radis picker,Search。所以當我點擊「顯示我」按鈕時,我想顯示用戶的當前位置。是的,我可以隱藏我的UIview來顯示整個mapView。是的,我只想在點擊按鈕時將地圖居中在用戶位置..... –