From 5d09ef6a44a38f3bcb132bc5b4eee298e85aedd5 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Tue, 4 Jun 2013 22:36:05 +0300 Subject: [PATCH] [api] Added zoom level support, minor renamings --- map/framework.hpp | 4 +-- map/map_tests/mwm_url_tests.cpp | 28 +++++++++++++++------ map/mwm_url.cpp | 43 ++++++++++++++++++++++----------- map/mwm_url.hpp | 14 ++++++----- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/map/framework.hpp b/map/framework.hpp index dd893c1a66..d3d1a32a65 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -465,8 +465,8 @@ public: void MapApiSetUriAndParse(string const & url); bool GetMapApiPoint(m2::PointD const & pxPoint, url_scheme::ApiPoint & point); vector const & GetMapApiPoints() { return m_ParsedMapApi.GetPoints(); } - void ClearMapApiPoints() { m_ParsedMapApi.Clear(); } - int GetMapApiVersion() const { return m_ParsedMapApi.GetApiversion(); } + void ClearMapApiPoints() { m_ParsedMapApi.Reset(); } + int GetMapApiVersion() const { return m_ParsedMapApi.GetApiVersion(); } string const & GetMapApiAppTitle() const { return m_ParsedMapApi.GetAppTitle(); } string const & GetMapApiBackUrl() const { return m_ParsedMapApi.GetGlobalBackUrl(); } m2::RectD GetMapApiRect() const { return m_ParsedMapApi.GetRect(); } diff --git a/map/map_tests/mwm_url_tests.cpp b/map/map_tests/mwm_url_tests.cpp index 3b4078ca73..d2b339c503 100644 --- a/map/map_tests/mwm_url_tests.cpp +++ b/map/map_tests/mwm_url_tests.cpp @@ -133,23 +133,23 @@ UNIT_TEST(VersionTest) { { ParsedMapApi api(Uri("mwm://map?ll=1,2&v=1&n=PointName")); - TEST_EQUAL(api.GetApiversion(), 1, ()); + TEST_EQUAL(api.GetApiVersion(), 1, ()); } { ParsedMapApi api(Uri("mwm://map?ll=1,2&v=kotik&n=PointName")); - TEST_EQUAL(api.GetApiversion(), 0, ()); + TEST_EQUAL(api.GetApiVersion(), 0, ()); } { ParsedMapApi api(Uri("mwm://map?ll=1,2&v=APacanyVoobsheKotjata&n=PointName")); - TEST_EQUAL(api.GetApiversion(), 0, ()); + TEST_EQUAL(api.GetApiVersion(), 0, ()); } { ParsedMapApi api(Uri("mwm://map?ll=1,2&n=PointName")); - TEST_EQUAL(api.GetApiversion(), 0, ()); + TEST_EQUAL(api.GetApiVersion(), 0, ()); } { - ParsedMapApi api(Uri("mwm://map?v=666&ll=1,2&n=PointName")); - TEST_EQUAL(api.GetApiversion(), 666, ()); + ParsedMapApi api(Uri("mwm://map?V=666&ll=1,2&n=PointName")); + TEST_EQUAL(api.GetApiVersion(), 666, ()); } } @@ -259,7 +259,7 @@ void generateRandomTest(size_t numberOfPoints, size_t stringLength) TEST_EQUAL(points[i].m_title, vect[i].m_title, ()); TEST_EQUAL(points[i].m_url, vect[i].m_url, ()); } - TEST_EQUAL(api.GetApiversion(), 1, ()); + TEST_EQUAL(api.GetApiVersion(), 1, ()); } } @@ -273,3 +273,17 @@ UNIT_TEST(StressTestRandomTest) { generateRandomTest(10000, 100); } + +UNIT_TEST(MWMApiZoomLevelTest) +{ + m2::RectD const r1 = ParsedMapApi(Uri("mwm://map?ll=0,0")).GetRect(); + m2::RectD const r2 = ParsedMapApi(Uri("mwm://map?z=14.5&ll=0,0")).GetRect(); + TEST_NOT_EQUAL(r1, r2, ()); + m2::RectD const r3 = ParsedMapApi(Uri("mwm://map?ll=0,0&z=14")).GetRect(); + TEST_NOT_EQUAL(r2, r3, ()); + TEST_NOT_EQUAL(r1, r3, ()); + m2::RectD const rEqualToR3 = ParsedMapApi(Uri("mwm://map?ll=0,0&z=14.000")).GetRect(); + TEST_EQUAL(r3, rEqualToR3, ()); + m2::RectD const rEqualToR1 = ParsedMapApi(Uri("mwm://map?ll=0,0&z=-23.43")).GetRect(); + TEST_EQUAL(r1, rEqualToR1, ()); +} diff --git a/map/mwm_url.cpp b/map/mwm_url.cpp index 84f84318c1..306b7f7686 100644 --- a/map/mwm_url.cpp +++ b/map/mwm_url.cpp @@ -1,6 +1,7 @@ #include "mwm_url.hpp" #include "../indexer/mercator.hpp" +#include "../indexer/scales.hpp" #include "../coding/uri.hpp" @@ -21,20 +22,19 @@ bool IsInvalidApiPoint(ApiPoint const & p) { return p.m_lat == INVALID_LAT_VALUE } // unnames namespace -ParsedMapApi::ParsedMapApi():m_id(0) -{} +ParsedMapApi::ParsedMapApi() : m_version(0), m_zoomLevel(0.0) +{ +} -ParsedMapApi::ParsedMapApi(Uri const & uri):m_id(0) +ParsedMapApi::ParsedMapApi(Uri const & uri) : m_version(0), m_zoomLevel(0.0) { if (!Parse(uri)) - { - m_points.clear(); - } + Reset(); } bool ParsedMapApi::SetUriAndParse(string const & url) { - Clear(); + Reset(); return Parse(url_scheme::Uri(url)); } @@ -55,7 +55,7 @@ bool ParsedMapApi::Parse(Uri const & uri) return true; } -void ParsedMapApi::AddKeyValue(string const & key, string const & value) +void ParsedMapApi::AddKeyValue(string key, string const & value) { strings::AsciiToLower(key); @@ -95,6 +95,11 @@ void ParsedMapApi::AddKeyValue(string const & key, string const & value) m_points.back().m_lon = lon; m_showRect = m2::Add(m_showRect, m2::PointD(lon, lat)); } + else if (key == "z") + { + if (!strings::to_double(value, m_zoomLevel)) + m_zoomLevel = 0.0; + } else if (key == "n") { if (!m_points.empty()) @@ -109,11 +114,6 @@ void ParsedMapApi::AddKeyValue(string const & key, string const & value) else LOG(LWARNING, ("Map API: Point url with no point. 'll' should come first!")); } -} - -void ParsedMapApi::Clear() -{ - m_points.clear(); else if (key == "backurl") { m_globalBackUrl = value; @@ -127,8 +127,23 @@ void ParsedMapApi::Clear() { m_appTitle = value; } +} + +void ParsedMapApi::Reset() +{ + m_points.clear(); m_globalBackUrl.clear(); m_appTitle.clear(); - m_id = 0; + m_version = 0; m_showRect = m2::RectD(); + m_zoomLevel = 0.0; +} + +m2::RectD ParsedMapApi::GetRect() const +{ + // Use zoom only for one point and ignore it for several points + if (m_zoomLevel >= 1.0 && m_points.size() == 1) + return scales::GetRectForLevel(m_zoomLevel, m_showRect.Center(), 1.0); + + return m_showRect; } diff --git a/map/mwm_url.hpp b/map/mwm_url.hpp index 45976a4ae9..1e999daed8 100644 --- a/map/mwm_url.hpp +++ b/map/mwm_url.hpp @@ -25,20 +25,22 @@ public: vector const & GetPoints() const { return m_points; } string const & GetGlobalBackUrl() const { return m_globalBackUrl; } string const & GetAppTitle() const { return m_appTitle; } - int GetApiversion() const { return m_id; } - m2::RectD GetRect() const { return m_showRect; } - void Clear(); + int GetApiVersion() const { return m_version; } + m2::RectD GetRect() const; + void Reset(); private: bool Parse(Uri const & uri); - void AddKeyValue(string const & key, string const & value); + void AddKeyValue(string key, string const & value); vector m_points; string m_globalBackUrl; string m_appTitle; - int m_id; - //Lon Lat coordinates + int m_version; m2::RectD m_showRect; + /// Zoom level in OSM format (e.g. from 1.0 to 20.0) + /// Taken into an account when calculating GetRect(), but only if points count is == 1 + double m_zoomLevel; }; }