How to move MKMapView based on selected annotation












11















I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin will appear in the centre of the visible area of the map.



Difficult to explain but hopefully it makes sense!
Thanks in advance.










share|improve this question



























    11















    I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin will appear in the centre of the visible area of the map.



    Difficult to explain but hopefully it makes sense!
    Thanks in advance.










    share|improve this question

























      11












      11








      11


      3






      I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin will appear in the centre of the visible area of the map.



      Difficult to explain but hopefully it makes sense!
      Thanks in advance.










      share|improve this question














      I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin will appear in the centre of the visible area of the map.



      Difficult to explain but hopefully it makes sense!
      Thanks in advance.







      cocoa-touch mkmapview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 15 '11 at 0:59









      RickyRicky

      2,1021526




      2,1021526
























          3 Answers
          3






          active

          oldest

          votes


















          31














          You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.



          For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:



          MKMapRect r = [mapView visibleMapRect];
          MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
          r.origin.x = pt.x - r.size.width * 0.5;
          r.origin.y = pt.y - r.size.height * 0.25;
          [mapView setVisibleMapRect:r animated:YES];





          share|improve this answer
























          • Thanks for that, Anomie. It worked great!

            – Ricky
            Mar 15 '11 at 3:28



















          0














          I ended up with following procedures:



          ** Centering on annotation:**



          - (void) centerOnSelection:(id<MKAnnotation>)annotation
          {
          MKCoordinateRegion region = self.mapView.region;
          region.center = annotation.coordinate;

          CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
          region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

          [self.mapView setRegion:region animated:YES];
          }


          ** Zooming on annotation:**



          - (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
          {
          DLog(@"zoomAndCenterOnSelection");

          MKCoordinateRegion region = self.mapView.region;
          MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

          region.center = annotation.coordinate;

          CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
          region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

          region.span = span;

          [self.mapView setRegion:region animated:YES];
          }


          -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides






          share|improve this answer































            0














            Using MKMapViewDelegate method:



            func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

            // center the mapView on the selected pin
            let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
            mapView.setRegion(region, animated: true)
            }





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5306392%2fhow-to-move-mkmapview-based-on-selected-annotation%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              31














              You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.



              For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:



              MKMapRect r = [mapView visibleMapRect];
              MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
              r.origin.x = pt.x - r.size.width * 0.5;
              r.origin.y = pt.y - r.size.height * 0.25;
              [mapView setVisibleMapRect:r animated:YES];





              share|improve this answer
























              • Thanks for that, Anomie. It worked great!

                – Ricky
                Mar 15 '11 at 3:28
















              31














              You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.



              For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:



              MKMapRect r = [mapView visibleMapRect];
              MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
              r.origin.x = pt.x - r.size.width * 0.5;
              r.origin.y = pt.y - r.size.height * 0.25;
              [mapView setVisibleMapRect:r animated:YES];





              share|improve this answer
























              • Thanks for that, Anomie. It worked great!

                – Ricky
                Mar 15 '11 at 3:28














              31












              31








              31







              You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.



              For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:



              MKMapRect r = [mapView visibleMapRect];
              MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
              r.origin.x = pt.x - r.size.width * 0.5;
              r.origin.y = pt.y - r.size.height * 0.25;
              [mapView setVisibleMapRect:r animated:YES];





              share|improve this answer













              You could try getting the MKMapRect from visibleMapRect for the map view, converting the annotation's coordinate to an MKMapPoint, resetting the MKMapRect's origin so the MKMapPoint is in the appropriate position, and then using setVisibleMapRect:animated: to set the visible region to the new MKMapRect.



              For example, if you wanted to move the map so the annotation is centered horizontally and 25% of the way down vertically, you could do something like this:



              MKMapRect r = [mapView visibleMapRect];
              MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
              r.origin.x = pt.x - r.size.width * 0.5;
              r.origin.y = pt.y - r.size.height * 0.25;
              [mapView setVisibleMapRect:r animated:YES];






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 15 '11 at 2:16









              AnomieAnomie

              73.1k13114136




              73.1k13114136













              • Thanks for that, Anomie. It worked great!

                – Ricky
                Mar 15 '11 at 3:28



















              • Thanks for that, Anomie. It worked great!

                – Ricky
                Mar 15 '11 at 3:28

















              Thanks for that, Anomie. It worked great!

              – Ricky
              Mar 15 '11 at 3:28





              Thanks for that, Anomie. It worked great!

              – Ricky
              Mar 15 '11 at 3:28













              0














              I ended up with following procedures:



              ** Centering on annotation:**



              - (void) centerOnSelection:(id<MKAnnotation>)annotation
              {
              MKCoordinateRegion region = self.mapView.region;
              region.center = annotation.coordinate;

              CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
              region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

              [self.mapView setRegion:region animated:YES];
              }


              ** Zooming on annotation:**



              - (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
              {
              DLog(@"zoomAndCenterOnSelection");

              MKCoordinateRegion region = self.mapView.region;
              MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

              region.center = annotation.coordinate;

              CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
              region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

              region.span = span;

              [self.mapView setRegion:region animated:YES];
              }


              -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides






              share|improve this answer




























                0














                I ended up with following procedures:



                ** Centering on annotation:**



                - (void) centerOnSelection:(id<MKAnnotation>)annotation
                {
                MKCoordinateRegion region = self.mapView.region;
                region.center = annotation.coordinate;

                CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
                region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

                [self.mapView setRegion:region animated:YES];
                }


                ** Zooming on annotation:**



                - (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
                {
                DLog(@"zoomAndCenterOnSelection");

                MKCoordinateRegion region = self.mapView.region;
                MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

                region.center = annotation.coordinate;

                CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
                region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

                region.span = span;

                [self.mapView setRegion:region animated:YES];
                }


                -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides






                share|improve this answer


























                  0












                  0








                  0







                  I ended up with following procedures:



                  ** Centering on annotation:**



                  - (void) centerOnSelection:(id<MKAnnotation>)annotation
                  {
                  MKCoordinateRegion region = self.mapView.region;
                  region.center = annotation.coordinate;

                  CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
                  region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

                  [self.mapView setRegion:region animated:YES];
                  }


                  ** Zooming on annotation:**



                  - (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
                  {
                  DLog(@"zoomAndCenterOnSelection");

                  MKCoordinateRegion region = self.mapView.region;
                  MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

                  region.center = annotation.coordinate;

                  CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
                  region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

                  region.span = span;

                  [self.mapView setRegion:region animated:YES];
                  }


                  -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides






                  share|improve this answer













                  I ended up with following procedures:



                  ** Centering on annotation:**



                  - (void) centerOnSelection:(id<MKAnnotation>)annotation
                  {
                  MKCoordinateRegion region = self.mapView.region;
                  region.center = annotation.coordinate;

                  CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
                  region.center.latitude -= self.mapView.region.span.latitudeDelta * per;

                  [self.mapView setRegion:region animated:YES];
                  }


                  ** Zooming on annotation:**



                  - (void) zoomAndCenterOnSelection:(id<MKAnnotation>)annotation
                  {
                  DLog(@"zoomAndCenterOnSelection");

                  MKCoordinateRegion region = self.mapView.region;
                  MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

                  region.center = annotation.coordinate;

                  CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
                  region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;

                  region.span = span;

                  [self.mapView setRegion:region animated:YES];
                  }


                  -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '15 at 17:01









                  altagiraltagir

                  485511




                  485511























                      0














                      Using MKMapViewDelegate method:



                      func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

                      // center the mapView on the selected pin
                      let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
                      mapView.setRegion(region, animated: true)
                      }





                      share|improve this answer




























                        0














                        Using MKMapViewDelegate method:



                        func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

                        // center the mapView on the selected pin
                        let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
                        mapView.setRegion(region, animated: true)
                        }





                        share|improve this answer


























                          0












                          0








                          0







                          Using MKMapViewDelegate method:



                          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

                          // center the mapView on the selected pin
                          let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
                          mapView.setRegion(region, animated: true)
                          }





                          share|improve this answer













                          Using MKMapViewDelegate method:



                          func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

                          // center the mapView on the selected pin
                          let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
                          mapView.setRegion(region, animated: true)
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 5:50









                          Justin VallelyJustin Vallely

                          2,4981831




                          2,4981831






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5306392%2fhow-to-move-mkmapview-based-on-selected-annotation%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Lugert, Oklahoma