Change Viewpoint

Demonstrates different ways in which you can change the Viewpoint, visible area, of an ArcGISMap.

How it works

To change the Viewpoint:

  1. Create an ArcGISMap.
  2. Set the map to the MapView, MapView.setMap().
  3. Change the view point with MapView.setViewPoint() or any method mention above.


 * Copyright 2017 Esri.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.

package com.esri.samples.mapview.change_viewpoint;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;

public class ChangeViewpointSample extends Application {

  private MapView mapView;
  private SpatialReference spatialReference;

  private static final int SCALE = 5000;

  public void start(Stage stage) throws Exception {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Change Viewpoint Sample");

      // create a control panel
      VBox vBoxControl = new VBox(6);
      vBoxControl.setMaxSize(260, 120);

      // create buttons for interaction
      Button animateButton = new Button("LONDON (Animate)");
      Button centerButton = new Button("WATERLOO (Center and Scaled)");
      Button geometryButton = new Button("WESTMINSTER (Geometry)");

      animateButton.setOnAction(e -> {
        // create the London location point
        Point londonPoint = new Point(-14093, 6711377, spatialReference);
        // create the viewpoint with the London point and scale
        Viewpoint viewpoint = new Viewpoint(londonPoint, SCALE);
        // set the map views's viewpoint to London with a seven second duration
        mapView.setViewpointAsync(viewpoint, 7);

      centerButton.setOnAction(e -> {
        // create the Waterloo location point
        Point waterlooPoint = new Point(-12153, 6710527, spatialReference);
        // set the map views's viewpoint centered on Waterloo and scaled
        mapView.setViewpointCenterAsync(waterlooPoint, SCALE);

      geometryButton.setOnAction(e -> {
        // create a collection of points around Westminster
        PointCollection westminsterPoints = new PointCollection(spatialReference);
        westminsterPoints.add(new Point(-13823, 6710390));
        westminsterPoints.add(new Point(-13823, 6710150));
        westminsterPoints.add(new Point(-14680, 6710390));
        westminsterPoints.add(new Point(-14680, 6710150));

        Polyline geometry = new Polyline(westminsterPoints);

        // set the map views's viewpoint to Westminster

      // add controls to the user interface panel
      vBoxControl.getChildren().addAll(animateButton, centerButton, geometryButton);

      // create ArcGISMap with imagery basemap
      ArcGISMap map = new ArcGISMap(Basemap.createImageryWithLabels());

      // create a view and set ArcGISMap to it
      mapView = new MapView();

      // create spatial reference for all points
      spatialReference = SpatialReferences.getWebMercator();
      // create point for starting location
      Point startPoint = new Point(-14093, 6711377, spatialReference);

      // set viewpoint of map view to starting point and scaled
      mapView.setViewpointCenterAsync(startPoint, SCALE);

      // add map view and control panel to stack pane
      stackPane.getChildren().addAll(mapView, vBoxControl);
      StackPane.setAlignment(vBoxControl, Pos.TOP_LEFT);
      StackPane.setMargin(vBoxControl, new Insets(10, 0, 0, 10));
    } catch (Exception e) {
      // on any error, print the stack trace

   * Stops and releases all resources used in application.
  public void stop() throws Exception {

    // release resources when the application closes
    if (mapView != null) {

   * Opens and runs application.
   * @param args arguments passed to this application
  public static void main(String[] args) {



In this topic
  1. How it works
  2. Code