Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:24:50+00:00 2026-06-18T15:24:50+00:00

I want to keep the map center coordinates into some limits. For this I

  • 0

I want to keep the map center coordinates into some limits. For this I call the setCenter method inside my "center" related callback function:

map.addObserver("center", function (obj, key, newValue, oldValue) {

  var limits = {minLat: 47.4136, minLon: 5.9845, maxLat: 54.8073, maxLon: 14.3671};

  if (newValue.latitude < limits.minLat || newValue.longitude < limits.minLon || 
      newValue.latitude > limits.maxLat || newValue.longitude > limits.maxLon) {

      var newLatLon = {latitude:  Math.max(Math.min(newValue.latitude, limits.maxLat), limits.minLat),
                       longitude: Math.max(Math.min(newValue.longitude, limits.maxLon), limits.minLon)};

      map.setCenter(nokia.maps.geo.Coordinate.fromObject(newLatLon));

      console.log(newValue);
      console.log(map.center);
  }
});

If I drag the map outside the limits, I see in the console the map.center being correctly adjusted, but the newValue coordinates keep being outisde the limits.

Did I misunderstand the API ?

I am using http://api.maps.nokia.com/2.2.3/jsl.js?with=all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:24:51+00:00Added an answer on June 18, 2026 at 3:24 pm

    Adding an observer to a property and then altering that property within the observer function is not guaranteed to work for all properties. My understanding is that re-setting the center is not supported in order to avoid infinite loops. You would be better off using the event framework rather than observers in this case.

    The code below restricts the centre of the map to remain within a rectangle centred on Germany. If you drag the map it will stop dead, if you flick the map it will bounce back. You will need to obtain your own free app id and token to get it to work.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />
            <title>Nokia Maps API Example: Restricted Area</title>
            <!-- KML support is required here. -->
            <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js"></script>
    
    
    </head>
    
    
    <style type="text/css">
                html {
                    overflow:hidden;
                }
    
                body {
                    margin: 0;
                    padding: 0;
                    overflow: hidden;
                    width: 100%;
                    height: 100%;
                    position: absolute;
                }
    
                #mapContainer {
                    width: 80%;
                    height: 80%;
                }
            </style>
        </head>
        <body>      
            <div id="mapContainer"></div>
            <script type="text/javascript">
    /////////////////////////////////////////////////////////////////////////////////////
    // Don't forget to set your API credentials
    //
    // Replace with your appId and token which you can obtain when you 
    // register on http://api.developer.nokia.com/ 
    //
                nokia.Settings.set( "appId", "APP ID"); 
                nokia.Settings.set( "authenticationToken", "TOKEN");
    //          
    /////////////////////////////////////////////////////////////////////////////////////   
            </script>
            <div id="uiContainer"></div>
            <script>
    
    var mapContainer = document.getElementById("mapContainer");
    var map = new nokia.maps.map.Display(mapContainer, {
        center: [51, 7],
        zoomLevel: 6
        });
    
    map.addComponent(new nokia.maps.map.component.Behavior());  
    var dragger = map.getComponentById("panning.Drag");    
    
    
    // Set of initial geo coordinates to create the purple polyline
    var points = [
            new nokia.maps.geo.Coordinate(47.4136, 5.9845),
            new nokia.maps.geo.Coordinate(47.4136, 14.3671),
            new nokia.maps.geo.Coordinate(54.8073, 14.3671),
            new nokia.maps.geo.Coordinate(54.8073, 5.9845),
            new nokia.maps.geo.Coordinate(47.4136, 5.9845)
        ];
    
    // Transparent purple polyline
    map.objects.add(new nokia.maps.map.Polyline(
        points,
        {   
            pen: {
                strokeColor: "#22CA", 
                lineWidth: 5
            }
        }
    )); 
    
    
    
    var restrict = function(evt) {   
    
         var limits = {minLat: 47.4136, minLon: 5.9845, maxLat: 54.8073, maxLon: 14.3671};
    
      if (map.center.latitude < limits.minLat || map.center.longitude < limits.minLon || 
          map.center.latitude > limits.maxLat || map.center.longitude > limits.maxLon) {
    
          var latitude =  Math.max(Math.min(map.center.latitude, limits.maxLat), limits.minLat);
          var longitude = Math.max(Math.min(map.center.longitude, limits.maxLon), limits.minLon);    
          map.setCenter(new nokia.maps.geo.Coordinate(latitude,longitude));      
          evt.cancel();
      }
    }
    
    
    map.addListener("dragend", restrict);
    map.addListener("drag", restrict);
    map.addListener("mapviewchange", restrict);
    map.addListener("mapviewchangeend", restrict);
    map.addListener("mapviewchangestart", restrict);
    
    
     </script>
        </body>
    </html>
    

    I have added event listeners to five events here, dragend, drag, mapviewchange, mapviewchangeend and mapviewchangestart – depending upon the effect you require, you may not need them all. The line evt.cancel(); stops the event from being processed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want keep my js in this style. I want write a map in
I want to keep backup of my database and import it into different System?
I want to keep some data which is checkhed in array list. I added
Have a Map which contains objects that I want to keep in sync across
If I just want to keep things very simple, and just map my tables
I want to keep some totals for different accounts. In C++ I'd use STL
This is a very simple thing, so I want to keep it as simple
I want to keep my footer at the bottom of the page while keeping
i want to keep my app in sync with the Server. The communication between
I want to keep an element always visible, even when it should be scrolled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.