मैं अपने मानचित्र दृश्य को रीफ्रेश करने का प्रयास कर रहा हूं और डिवाइस को उपयोगकर्ता स्थान प्राप्त करते समय सर्वर से नया डेटा लोड करने का प्रयास कर रहा हूं। यहाँ मैं क्या कर रहा है:उपयोगकर्ता स्थान को अद्यतन या डिफ़ॉल्ट मानों का उपयोग करते समय MKMapView को रीफ्रेश करना
- (void)viewDidLoad {
CGRect bounds = self.view.bounds;
mapView = [[MKMapView alloc] initWithFrame:bounds];
mapView.showsUserLocation=YES;
mapView.delegate=self;
[self.view insertSubview:mapView atIndex:0];
[self refreshMap];
}
- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//this is to avoid frequent updates, I just need one position and don't need to continuously track the user's location
if (userLocation.location.horizontalAccuracy > self.myUserLocation.location.horizontalAccuracy) {
self.myUserLocation = userLocation;
CLLocationCoordinate2D centerCoord = { userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude };
[theMapView setCenterCoordinate:centerCoord zoomLevel:10 animated:YES];
[self refreshMap];
}
}
- (void)refreshMap {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self.mapView removeAnnotations:self.mapView.annotations];
//default position
NSString *lat = @"45.464161";
NSString *lon = @"9.190336";
if (self.myUserLocation != nil) {
lat = [[NSNumber numberWithDouble:myUserLocation.coordinate.latitude] stringValue];
lon = [[NSNumber numberWithDouble:myUserLocation.coordinate.longitude] stringValue];
}
NSString *url = [[NSString alloc] initWithFormat:@"http://myserver/geo_search.json?lat=%@&lon=%@", lat, lon];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[url release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
मैं भी एक है - (शून्य) कनेक्शन: (NSURLConnection *) कनेक्शन didReceiveData: (NSData *) डेटा, बना सकते हैं और टिप्पणियां जोड़ने के लिए।
यहां क्या होता है: कभी-कभी मुझे थोड़ी देर बाद स्थान मिलता है, इसलिए मैंने पहले से ही डिफ़ॉल्ट स्थान के करीब डेटा लोड किया है। फिर मुझे स्थान मिलता है और पुरानी टिप्पणियां हटाएं और नए जोड़ें, इसके बजाय मैं पुरानी एनोटेशन देख रहा हूं और नए भी जोड़े गए हैं (मान लें कि मुझे हर बार 10 एनोटेशन प्राप्त होते हैं, इसलिए इसका मतलब है कि मैं मेरे पास 20 है जब मेरे पास 10 होना चाहिए)।
मैं क्या गलत कर रहा हूं?
रीफ्रेश मैप में, निकालें लाइन से पहले, जांचें कि self.mapView शून्य है या नहीं। – Anna
यह शून्य नहीं है, यह मुझे मिलता है:> मैं ठीक से परीक्षण भी नहीं कर सकता क्योंकि मुझे केवल वास्तविक समस्या पर यह समस्या दिखाई देती है, सिम्युलेटर में नहीं ... क्या कोड तर्कसंगत रूप से सही है? –
koichirose
मुझे कोई स्पष्ट समस्या नहीं दिख रही है। शायद यह कुछ है कि एनोटेशन कैसे जोड़े गए हैं या देखने के लिए फॉरऑनोटेशन। – Anna