Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
fixes #97: add LatLng struct & refactor function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Sep 30, 2014
1 parent ee3e082 commit 7e7a896
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 45 deletions.
8 changes: 5 additions & 3 deletions common/glfw_view.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "glfw_view.hpp"

#include <mbgl/map/geography.h>
#include <mbgl/util/string.hpp>

GLFWView::GLFWView(bool fullscreen) : fullscreen(fullscreen) {
Expand Down Expand Up @@ -195,10 +196,11 @@ void GLFWView::make_active() {
void GLFWView::swap() {
glfwPostEmptyEvent();

double lon, lat, zoom;
map->getLonLatZoom(lon, lat, zoom);
mbgl::LatLng latLng;
double zoom;
map->getLatLngZoom(latLng, zoom);
const double bearing = map->getBearing();
const std::string title = mbgl::util::sprintf<128>("Mapbox GL – %.2f/%.6f/%.6f/%.1f", zoom, lat, lon, bearing);
const std::string title = mbgl::util::sprintf<128>("Mapbox GL – %.2f/%.6f/%.6f/%.1f", zoom, latLng.latitude, latLng.longitude, bearing);
glfwSetWindowTitle(window, title.c_str());
}

Expand Down
13 changes: 13 additions & 0 deletions include/mbgl/map/geography.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef MBGL_MAP_GEOGRAPHY
#define MBGL_MAP_GEOGRAPHY

namespace mbgl {

typedef struct {
double latitude;
double longitude;
} LatLng;

}

#endif
9 changes: 5 additions & 4 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef MBGL_MAP_MAP
#define MBGL_MAP_MAP

#include <mbgl/map/geography.h>
#include <mbgl/map/transform.hpp>
#include <mbgl/renderer/painter.hpp>

Expand Down Expand Up @@ -78,8 +79,8 @@ class Map : private util::noncopyable {

// Position
void moveBy(double dx, double dy, double duration = 0);
void setLonLat(double lon, double lat, double duration = 0);
void getLonLat(double &lon, double &lat) const;
void setLatLng(LatLng latLng, double duration = 0);
const LatLng getLatLng() const;
void startPanning();
void stopPanning();
void resetPosition();
Expand All @@ -90,8 +91,8 @@ class Map : private util::noncopyable {
double getScale() const;
void setZoom(double zoom, double duration = 0);
double getZoom() const;
void setLonLatZoom(double lon, double lat, double zoom, double duration = 0);
void getLonLatZoom(double &lon, double &lat, double &zoom) const;
void setLatLngZoom(LatLng latLng, double zoom, double duration = 0);
void getLatLngZoom(LatLng &latLng, double &zoom) const;
void resetZoom();
void startScaling();
void stopScaling();
Expand Down
9 changes: 5 additions & 4 deletions include/mbgl/map/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MBGL_MAP_TRANSFORM

#include <mbgl/util/time.hpp>
#include <mbgl/map/geography.h>
#include <mbgl/map/transform_state.hpp>

#include <mbgl/util/noncopyable.hpp>
Expand All @@ -28,10 +29,10 @@ class Transform : private util::noncopyable {

// Position
void moveBy(double dx, double dy, timestamp duration = 0);
void setLonLat(double lon, double lat, timestamp duration = 0);
void setLonLatZoom(double lon, double lat, double zoom, timestamp duration = 0);
void getLonLat(double& lon, double& lat) const;
void getLonLatZoom(double& lon, double& lat, double& zoom) const;
void setLatLng(LatLng latLng, timestamp duration = 0);
void setLatLngZoom(LatLng latLng, double zoom, timestamp duration = 0);
const LatLng getLatLng() const;
void getLatLngZoom(LatLng &latLng, double& zoom) const;
void startPanning();
void stopPanning();

Expand Down
2 changes: 1 addition & 1 deletion ios/mapbox-gl-cocoa
4 changes: 2 additions & 2 deletions linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) {

// Load settings
mbgl::Settings_JSON settings;
map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
map.setLatLngZoom({ settings.latitude, settings.longitude }, settings.zoom);
map.setBearing(settings.bearing);
map.setDebug(settings.debug);

Expand All @@ -70,7 +70,7 @@ int main(int argc, char *argv[]) {
int ret = view->run();

// Save settings
map.getLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
map.getLatLngZoom({ settings.latitude, settings.longitude }, settings.zoom);
settings.bearing = map.getBearing();
settings.debug = map.getDebug();
settings.save();
Expand Down
24 changes: 15 additions & 9 deletions macosx/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "../common/glfw_view.hpp"
#include "../common/nslog_log.hpp"

#include <mbgl/map/geography.h>

#import <Foundation/Foundation.h>

@interface URLHandler : NSObject
Expand All @@ -26,15 +28,16 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
[params setObject:[parts objectAtIndex:1] forKey:[parts objectAtIndex:0]];
}

double latitude = 0, longitude = 0, zoom = 0, bearing = 0;
mbgl::LatLng latLng = { 0, 0 };
double zoom = 0, bearing = 0;
bool hasCenter = false, hasZoom = false, hasBearing = false;

NSString *centerString = [params objectForKey:@"center"];
if (centerString) {
NSArray *latlon = [centerString componentsSeparatedByString:@","];
if ([latlon count] == 2) {
latitude = [[latlon objectAtIndex:0] doubleValue];
longitude = [[latlon objectAtIndex:1] doubleValue];
NSArray *latLngValues = [centerString componentsSeparatedByString:@","];
if ([latLngValues count] == 2) {
latLng.latitude = [latLngValues[0] doubleValue];
latLng.longitude = [latLngValues[1] doubleValue];
hasCenter = true;
}
}
Expand All @@ -53,9 +56,9 @@ - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event

if ([self map]) {
if (hasCenter && hasZoom) {
[self map]->setLonLatZoom(longitude, latitude, zoom);
[self map]->setLatLngZoom(latLng, zoom);
} else if (hasCenter) {
[self map]->setLonLat(longitude, latitude);
[self map]->setLatLng(latLng);
} else if (hasZoom) {
[self map]->setZoom(zoom);
}
Expand All @@ -80,7 +83,7 @@ int main() {

// Load settings
mbgl::Settings_NSUserDefaults settings;
map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
map.setLatLngZoom({ settings.latitude, settings.longitude }, settings.zoom);
map.setBearing(settings.bearing);
map.setDebug(settings.debug);

Expand All @@ -98,7 +101,10 @@ int main() {
int ret = view.run();

// Save settings
map.getLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
mbgl::LatLng latLng;
map.getLatLngZoom(latLng, settings.zoom);
settings.latitude = latLng.latitude;
settings.longitude = latLng.longitude;
settings.bearing = map.getBearing();
settings.debug = map.getDebug();
settings.save();
Expand Down
18 changes: 9 additions & 9 deletions src/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ void Map::moveBy(double dx, double dy, double duration) {
}

// Note: This function is called from another thread. Make sure you only call threadsafe functions!
void Map::setLonLat(double lon, double lat, double duration) {
transform.setLonLat(lon, lat, duration * 1_second);
void Map::setLatLng(LatLng latLng, double duration) {
transform.setLatLng(latLng, duration * 1_second);
update();
}

// Note: This function is called from another thread. Make sure you only call threadsafe functions!
void Map::getLonLat(double& lon, double& lat) const {
transform.getLonLat(lon, lat);
const LatLng Map::getLatLng() const {
return transform.getLatLng();
}

// Note: This function is called from another thread. Make sure you only call threadsafe functions!
Expand All @@ -297,7 +297,7 @@ void Map::stopPanning() {
// Note: This function is called from another thread. Make sure you only call threadsafe functions!
void Map::resetPosition() {
transform.setAngle(0);
transform.setLonLat(0, 0);
transform.setLatLng({ 0, 0 });
transform.setZoom(0);
update();
}
Expand Down Expand Up @@ -334,14 +334,14 @@ double Map::getZoom() const {
}

// Note: This function is called from another thread. Make sure you only call threadsafe functions!
void Map::setLonLatZoom(double lon, double lat, double zoom, double duration) {
transform.setLonLatZoom(lon, lat, zoom, duration * 1_second);
void Map::setLatLngZoom(LatLng latLng, double zoom, double duration) {
transform.setLatLngZoom(latLng, zoom, duration * 1_second);
update();
}

// Note: This function is called from another thread. Make sure you only call threadsafe functions!
void Map::getLonLatZoom(double& lon, double& lat, double& zoom) const {
transform.getLonLatZoom(lon, lat, zoom);
void Map::getLatLngZoom(LatLng &latLng, double& zoom) const {
transform.getLatLngZoom(latLng, zoom);
}

// Note: This function is called from another thread. Make sure you only call threadsafe functions!
Expand Down
27 changes: 15 additions & 12 deletions src/map/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,17 @@ void Transform::_moveBy(const double dx, const double dy, const timestamp durati
duration);
}

void Transform::setLonLat(const double lon, const double lat, const timestamp duration) {
void Transform::setLatLng(const LatLng latLng, const timestamp duration) {
uv::writelock lock(mtx);

const double f = std::fmin(std::fmax(std::sin(D2R * lat), -0.9999), 0.9999);
double xn = -lon * Bc;
const double f = std::fmin(std::fmax(std::sin(D2R * latLng.latitude), -0.9999), 0.9999);
double xn = -latLng.longitude * Bc;
double yn = 0.5 * Cc * std::log((1 + f) / (1 - f));

_setScaleXY(current.scale, xn, yn, duration);
}

void Transform::setLonLatZoom(const double lon, const double lat, const double zoom,
const timestamp duration) {
void Transform::setLatLngZoom(const LatLng latLng, const double zoom, const timestamp duration) {
uv::writelock lock(mtx);

double new_scale = std::pow(2.0, zoom);
Expand All @@ -113,22 +112,26 @@ void Transform::setLonLatZoom(const double lon, const double lat, const double z
Bc = s / 360;
Cc = s / (2 * M_PI);

const double f = std::fmin(std::fmax(std::sin(D2R * lat), -0.9999), 0.9999);
double xn = -lon * Bc;
const double f = std::fmin(std::fmax(std::sin(D2R * latLng.latitude), -0.9999), 0.9999);
double xn = -latLng.longitude * Bc;
double yn = 0.5 * Cc * log((1 + f) / (1 - f));

_setScaleXY(new_scale, xn, yn, duration);
}

void Transform::getLonLat(double &lon, double &lat) const {
const LatLng Transform::getLatLng() const {
uv::readlock lock(mtx);

lon = -final.x / Bc;
lat = R2D * (2 * std::atan(std::exp(final.y / Cc)) - 0.5 * M_PI);
LatLng ll;

ll.longitude = -final.x / Bc;
ll.latitude = R2D * (2 * std::atan(std::exp(final.y / Cc)) - 0.5 * M_PI);

return ll;
}

void Transform::getLonLatZoom(double &lon, double &lat, double &zoom) const {
getLonLat(lon, lat);
void Transform::getLatLngZoom(LatLng &latLng, double &zoom) const {
latLng = getLatLng();

uv::readlock lock(mtx);
zoom = getZoom();
Expand Down
2 changes: 1 addition & 1 deletion test/headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ TEST_P(HeadlessTest, render) {

view.resize(width, height, pixelRatio);
map.resize(width, height, pixelRatio);
map.setLonLatZoom(longitude, latitude, zoom);
map.setLatLngZoom({ latitude, longitude }, zoom);
map.setBearing(bearing);

// Run the loop. It will terminate when we don't have any further listeners.
Expand Down

0 comments on commit 7e7a896

Please sign in to comment.