Qt-based BB10 API Examples Documentation

MapViewDemo.cpp Example File

custommapview/src/MapViewDemo.cpp
    /* Copyright (c) 2012, 2013  BlackBerry Limited.
    *
    * 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
    *
    * http://www.apache.org/licenses/LICENSE-2.0
    *
    * 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.
    */
    #include "MapViewDemo.hpp"

    #include <bb/cascades/AbstractPane>
    #include <bb/cascades/Application>
    #include <bb/cascades/Container>
    #include <bb/cascades/maps/MapView>
    #include <bb/cascades/QmlDocument>
    #include <bb/platform/geo/Point.hpp>

    #include <QPoint>

    using namespace bb::cascades;
    using namespace bb::cascades::maps;
    using namespace bb::platform::geo;

    MapViewDemo::MapViewDemo(bb::cascades::Application *app) :
            QObject(app)
    {
        // create scene document from main.qml asset
        // set parent to created document to ensure it exists for the whole application lifetime
        QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
        qml->setContextProperty("_mapViewTest", this);

        // create root object for the UI
        AbstractPane *root = qml->createRootObject<AbstractPane>();

        // set created root object as a scene
        app->setScene(root);
    }

    QVariantList MapViewDemo::worldToPixelInvokable(QObject* mapObject, double latitude, double longitude) const
    {
        MapView* mapview = qobject_cast<MapView*>(mapObject);
        const Point worldCoordinates = Point(latitude, longitude);
        const QPoint pixels = mapview->worldToWindow(worldCoordinates);

        return QVariantList() << pixels.x() << pixels.y();
    }

    void MapViewDemo::updateMarkers(QObject* mapObject, QObject* containerObject) const
    {
        MapView* mapview = qobject_cast<MapView*>(mapObject);
        Container* container = qobject_cast<Container*>(containerObject);

        for (int i = 0; i < container->count(); i++) {
            const QPoint xy = worldToPixel(mapview,
                                           container->at(i)->property("lat").value<double>(),
                                           container->at(i)->property("lon").value<double>());
            container->at(i)->setProperty("x", xy.x());
            container->at(i)->setProperty("y", xy.y());
        }
    }

    QPoint MapViewDemo::worldToPixel(QObject* mapObject, double latitude, double longitude) const
    {
        MapView* mapview = qobject_cast<MapView*>(mapObject);
        const Point worldCoordinates = Point(latitude, longitude);

        return mapview->worldToWindow(worldCoordinates);
    }