Search code examples
javascripthere-apigeocode

How can I set a country using a Country Code with Nokia Maps?


I've read that you can search a country using a Country Code (I tried it with Nokia Here it's wonderful), the code from my country is "SV" (I'm from El Salvador) but I don't know how to implement. I found this codes in the API guide: Interface nokia.maps.search.Address, but they don't have any example.

Could you give me a very simple example how to implement? Because I couldn't find any example. And if you know how to set the automatic zoom in the country that you choose I'll appreciative too much. Thank you very much for your help and your valuable time.


Solution

  • The HERE maps geocoding service is described here in the documentation - note that the latest version of the API is version 2.2.4 not 2.1.1 referred to in your link.

    The latest library should be loaded using the following script

    <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.4/jsl.js?with=all"></script>
    

    Geocoding requests can be made using either a free text address or a structured address.

    For a free text search you make the request as shown:

    var searchTerm = "berlin";
    
    nokia.places.search.manager.geoCode({
        searchTerm: searchTerm,
        onComplete: processResults
    });
    

    This will find the largest city worldwide called BERLIN i.e. the one in Germany.

    For a structured address search you make the request as shown:

    var address = new Object();
    
    address.countryCode = "USA";
    address.city = "Berlin";
    
    nokia.places.search.manager.geoCode({
        address: address,
        onComplete: processResults
    });
    

    This will find the city of Berlin in New Hampshire USA. The address object can hold any of the attributes described in address

    Regarding zooming to a country - this is actually a type of findPlaces search - you are looking for the first (i.e. most important) result of a places search for an administrative region with the name of the country. The following code will zoom to El Salvador:

    var zoomTo = function (data, status) {
    
        if (status == "OK") {
            var locations = data.results.items;
            for (var i = 0, len = locations.length ;  i < len ; i++) {
    
                if ( "administrative-region"  == locations[i].category.categoryId){                             
                    map.zoomTo(nokia.maps.geo.BoundingBox.coverAll
                            ([locations[i].boundingBox.topLeft,
                             locations[i].boundingBox.bottomRight ]), false);
                    break;
                }
            }
        } else {
            alert("The search request failed");
        }
    };
    
    var term = "El Salvador";       
    nokia.places.search.manager.findPlaces({
        searchTerm: term,
        onComplete: zoomTo ,
        boundingBox: {
            topLeft: {
                latitude: 85,
                longitude: -179.99
            },
            bottomRight: {
                latitude: -85,
                longitude: 179.99
            }
        }
    });