एक MKMapView की सीमा से हो रही है

वोट
29

सेटअप करने के लिए आदेश में किसी बाहरी सर्वर एक प्रश्न मैं मैं निर्माण कर रहा हूँ एक iPhone app में वर्तमान मानचित्र दृश्य की सीमा से प्राप्त करना चाहते हैं। UIView सीमा को जवाब देना चाहिए, लेकिन यह MKMapView नहीं है लगता है। एक क्षेत्र की स्थापना और मानचित्र में ज़ूम करने के बाद मैं सीमा प्राप्त करने की कोशिश। मैं पहला कदम CGPoints कि नक्शे के एसई और एनडब्ल्यू कोनों प्रतिनिधित्व प्राप्त करने की कोशिश करने के लिए है, जिस पर अटक कर रहा हूँ। उसके बाद मैं उपयोग करने के लिए जा रहा था:

- (CLLocationCoordinate2D)convertPoint:(CGPoint)point toCoordinateFromView:(UIView *)view

मानचित्र निर्देशांक में अंक को बदलने के लिए। लेकिन मैं यह भी है कि अब तक प्राप्त नहीं कर सकते ...

//Recenter and zoom map in on search location
MKCoordinateRegion region =  {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:YES];


//After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
//First we need to calculate the corners of the map
CGPoint se = CGPointMake(self.mapView.bounds.origin.x, mapView.bounds.origin.y);
CGPoint nw = CGPointMake((self.mapView.bounds.origin.x + mapView.bounds.size.width), (mapView.bounds.origin.y + mapView.bounds.size.height));
NSLog(@points are: se %@, nw %@, se, nw);

कोड चेतावनी के बिना संकलित तथापि से और NW दोनों अशक्त हैं। NSLog की कोशिश self.mapView.bounds.origin.x को देखते हुए चर 0. है सीधे self.mapView.bounds.size.width मुझे एक कार्यक्रम संकेत प्राप्त: देता EXC_BAD_ACCESS । जो NSLog से आते हैं लगता है।

किसी को भी (मानचित्र निर्देशांक में) एक MKMapView के दृश्य क्षेत्र से दक्षिण पूर्वी कोने और उत्तर पश्चिमी किनारा पाने के लिए उचित तरीके से पता है?

संपादित करें: ऐसा लगता है जब भी आप कुछ पूछा यहाँ जवाब सही होने के बाद आप के लिए आता है। मैं% @f के बजाय @ उपयोग कर रहा था NSLog में प्रत्येक चर जो त्रुटियों वहाँ फेंक दिया गया था मुद्रित करने के लिए। मैं भी MKMapView की annotationVisibleRect संपत्ति की खोज की। ऐसा लगता है कि हालांकि annotationVisibleRect जनक दृश्य निर्देशांक पर आधारित है।

17/01/2010 को 17:53
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


10 जवाब

वोट
73

ठीक है, मैं आधिकारिक तौर पर अपने ही सवाल का जवाब दे लेकिन जब से मैं इसे कहीं भी नहीं मिला इससे पहले कि मैं जवाब यहाँ पोस्ट करेंगे:

//To calculate the search bounds...
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];

CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
17/01/2010 को 20:14
का स्रोत उपयोगकर्ता

वोट
38

एक अन्य विकल्प अपने MKMapView उदाहरण पर visibleMapRect संपत्ति का उपयोग करें और MKCoordinateForMapPoint () का उपयोग अक्षांश / देशांतर में कन्वर्ट करने के लिए है।

MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint neMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), mRect.origin.y);
MKMapPoint swMapPoint = MKMapPointMake(mRect.origin.x, MKMapRectGetMaxY(mRect));
CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(neMapPoint);
CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(swMapPoint);
06/12/2010 को 06:04
का स्रोत उपयोगकर्ता

वोट
9

दूर स्विफ्ट ... (@ deadroxy के जवाब के आधार पर ...)

typealias Edges = (ne: CLLocationCoordinate2D, sw: CLLocationCoordinate2D)

extension MKMapView {
    func edgePoints() -> Edges {
        let nePoint = CGPoint(x: self.bounds.maxX, y: self.bounds.origin.y)
        let swPoint = CGPoint(x: self.bounds.minX, y: self.bounds.maxY)
        let neCoord = self.convertPoint(nePoint, toCoordinateFromView: self)
        let swCoord = self.convertPoint(swPoint, toCoordinateFromView: self)
        return (ne: neCoord, sw: swCoord)
    }
}
23/02/2015 को 21:30
का स्रोत उपयोगकर्ता

वोट
3

यह http://wiki.openstreetmap.org/wiki/Bounding_Box बाउंडिंग बॉक्स के लिए एक दस्तावेज है

bbox = left,bottom,right,top
bbox = min Longitude , min Latitude , max Longitude , max Latitude

यहाँ छवि विवरण दर्ज

तुम एक हो सकता है BoundingBoxstruct है कि इस का प्रतिनिधित्व करता है

struct BoundingBox {
  let min: CLLocationCoordinate2D
  let max: CLLocationCoordinate2D

  init(rect: MKMapRect) {
    let bottomLeft = MKMapPointMake(rect.origin.x, MKMapRectGetMaxY(rect))
    let topRight = MKMapPointMake(MKMapRectGetMaxX(rect), rect.origin.y)

    min = MKCoordinateForMapPoint(bottomLeft)
    max = MKCoordinateForMapPoint(topRight)
  }

  var points: [CLLocationDegrees] {
    return [
      min.latitude,
      min.longitude,
      max.latitude
      max.longitude,
    ]
  }
}

visibleMapRectरूप में ही हैregion.span

let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 320, height: 640))
XCTAssertEqual(mapView.userLocation.coordinate.latitude, 0)
XCTAssertEqual(mapView.userLocation.coordinate.longitude, 0)

let boundingBox = BoundingBox(rect: mapView.visibleMapRect)
XCTAssertEqual(boundingBox.max.longitude-boundingBox.min.longitude, mapView.region.span.longitudeDelta)
XCTAssertEqual(boundingBox.max.latitude-boundingBox.min.latitude, mapView.region.span.latitudeDelta)
04/04/2017 को 07:34
का स्रोत उपयोगकर्ता

वोट
1

इस वेबसाइट समस्या का समाधान। http://www.softwarepassion.com/how-to-get-geographic-coordinates-of-the-visible-mkmapview-area-in-ios/

MKMapRect mRect = self.mapView.visibleMapRect;

-(CLLocationCoordinate2D)getNECoordinate:(MKMapRect)mRect{
    return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMaxX(mRect) y:mRect.origin.y];
}
-(CLLocationCoordinate2D)getNWCoordinate:(MKMapRect)mRect{
    return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMinX(mRect) y:mRect.origin.y];
}
-(CLLocationCoordinate2D)getSECoordinate:(MKMapRect)mRect{
    return [self getCoordinateFromMapRectanglePoint:MKMapRectGetMaxX(mRect) y:MKMapRectGetMaxY(mRect)];
}
-(CLLocationCoordinate2D)getSWCoordinate:(MKMapRect)mRect{
    return [self getCoordinateFromMapRectanglePoint:mRect.origin.x y:MKMapRectGetMaxY(mRect)];
}

-(CLLocationCoordinate2D)getCoordinateFromMapRectanglePoint:(double)x y:(double)y{
    MKMapPoint swMapPoint = MKMapPointMake(x, y);
    return MKCoordinateForMapPoint(swMapPoint);
}

-(NSArray *)getBoundingBox:(MKMapRect)mRect{
    CLLocationCoordinate2D bottomLeft = [self getSWCoordinate:mRect];
    CLLocationCoordinate2D topRight = [self getNECoordinate:mRect];
    return @[[NSNumber numberWithDouble:bottomLeft.latitude ],
             [NSNumber numberWithDouble:bottomLeft.longitude],
             [NSNumber numberWithDouble:topRight.latitude],
             [NSNumber numberWithDouble:topRight.longitude]];
}
18/12/2017 को 13:14
का स्रोत उपयोगकर्ता

वोट
1

मैं इस पाने के लिए पार्स GeoBox क्वेरी के साथ काम करने में सक्षम था:

//Calculate the corners of the map to get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + self.mapView.bounds.size.width, self.mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x),(self.mapView.bounds.origin.y+ self.mapView.bounds.size.height));

//Transform points into lat/long values
CLLocationCoordinate2D NECoordinate = [self.mapView convertPoint:nePoint toCoordinateFromView:self.mapView];
CLLocationCoordinate2D SWCoordinate = [self.mapView convertPoint:swPoint toCoordinateFromView:self.mapView];

//Convert to Parse GeoPoints
PFGeoPoint *Southwest = [PFGeoPoint geoPointWithLatitude:SWCoordinate.latitude longitude:SWCoordinate.longitude];
PFGeoPoint *Northeast = [PFGeoPoint geoPointWithLatitude:NECoordinate.latitude longitude:NECoordinate.longitude];
19/12/2014 को 14:39
का स्रोत उपयोगकर्ता

वोट
0

अपडेट किया गया @ मेरी प्रयोजनों के लिए onmyway133 उत्तम जवाब, मैं चारों कोनों के निर्देशांक की जरूरत:

struct BoundingBox {
    let topRight: CLLocationCoordinate2D
    let topLeft: CLLocationCoordinate2D
    let bottomRight: CLLocationCoordinate2D
    let bottomLeft: CLLocationCoordinate2D

    init(rect: MKMapRect) {
        topRight = MKMapPoint(x: rect.maxX, y: rect.origin.y).coordinate
        topLeft = MKMapPoint(x: rect.origin.x, y: rect.origin.y).coordinate
        bottomRight = MKMapPoint(x: rect.maxX, y: rect.maxY).coordinate
        bottomLeft = MKMapPoint(x: rect.origin.x, y: rect.maxY).coordinate
    }

    var items: [String: CLLocationCoordinate2D] {
        return [
            "topRight": topRight,
            "topLeft": topLeft,
            "bottomRight": bottomRight,
            "bottomLeft": bottomLeft,
        ]
    }

    var points: [CLLocationDegrees] {
        return [
            topRight.latitude,
            topRight.longitude,
            topLeft.latitude,
            topLeft.longitude,
            bottomRight.latitude,
            bottomRight.longitude,
            bottomLeft.latitude,
            bottomLeft.longitude,
        ]
    }
}

और मैं इस डेटा कैसे किया जाता का एक उदाहरण:

        let boundingBox = BoundingBox(rect: mapView.visibleMapRect)
        var annotations = Array<MKPointAnnotation>()

        for point in boundingBox.items {
            let newPoint = MKPointAnnotation()
            newPoint.coordinate = point.value
            annotations.append(newPoint)
        }

        mapView.addAnnotations(annotations)
27/05/2019 को 23:57
का स्रोत उपयोगकर्ता

वोट
0

इस विस्तार के इस समस्या का हल करने और बनाए रखने centerCoordinateस्विफ्ट 5 में वाक्य रचना

extension MKMapView {
    var northWestCoordinate: CLLocationCoordinate2D {
        return MKMapPoint(x: visibleMapRect.minX, y: visibleMapRect.minY).coordinate
    }

    var northEastCoordinate: CLLocationCoordinate2D {
        return MKMapPoint(x: visibleMapRect.maxX, y: visibleMapRect.minY).coordinate
    }

    var southEastCoordinate: CLLocationCoordinate2D {
        return MKMapPoint(x: visibleMapRect.maxX, y: visibleMapRect.maxY).coordinate
    }

    var southWestCoordinate: CLLocationCoordinate2D {
        return MKMapPoint(x: visibleMapRect.minX, y: visibleMapRect.maxY).coordinate
    }
}
17/04/2019 को 18:02
का स्रोत उपयोगकर्ता

वोट
0

मैं नक्शे है कि 2 उंगली घुमाया गया था के लिए अन्य उत्तर से कुछ के साथ कुछ परेशानी हो रही थी। इस कोड को मेरे लिए काम किया:

MKMapRect rect = self.mapView.visibleMapRect;
CLLocationCoordinate2D northeast = MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMaxX(rect),rect.origin.y));
CLLocationCoordinate2D southwest = MKCoordinateForMapPoint(MKMapPointMake(rect.origin.x         ,MKMapRectGetMaxY(rect)));

मेरा जवाब से ली गई है 陈保状के जवाब और संबद्ध वेबसाइट यहाँ लिंक विवरण दर्ज । इसके नीचे सरलीकृत भी 3 दक्षिण-पश्चिम और उत्तर पूर्वी कोने के लिए लाइनों।

24/08/2018 को 20:04
का स्रोत उपयोगकर्ता

वोट
0

इस कोड को नक्शे 90/180 डिग्री की तरह घूर्णन के साथ काम करता है। सेट mapView.pitchEnabled = नहीं; कम कीड़े के लिए।

CLLocationDirection heading = mapView.camera.heading;

float mapWidth = mapView.frame.size.width;
float mapHeight = mapView.frame.size.height;

float neX = mapWidth;
float neY = 0.0;

float swX = 0.0;
float swY = mapHeight;


if (heading >= 0 && heading <= 90) {
    //println("Q1")
    float ratio = heading / 90;

    neX = (1-ratio) * mapWidth;
    swX = (mapWidth*ratio);
} else if (heading >= 90 && heading <= 180) {
    //println("Q2")
    float ratio = (heading - 90) / 90;
    neX = 0;
    neY = (mapHeight*ratio);
    swY = (1-ratio) * mapHeight;
    swX = mapWidth;

} else if (heading >= 180 && heading <= 270) {
    //println("Q3")
    float ratio = (heading - 180) / 90;
    neX = mapWidth*ratio;
    neY = mapHeight;
    swX = (1-ratio) * mapWidth;
    swY = 0;

} else if (heading >= 270 && heading <= 360) {
    //println("Q4");
    float ratio = (heading - 270) / 90;
    neX = mapWidth;
    neY = (1-ratio) * mapHeight;
    swY = ratio * mapHeight;

}

CGPoint swPoint = CGPointMake(swX, swY);
CGPoint nePoint = CGPointMake(neX, neY);

CLLocationCoordinate2D swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
CLLocationCoordinate2D neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
15/10/2015 को 01:17
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more