examples: GoogleMaps

git-svn-id: svn://ultimatepp.org/upp/trunk@2037 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2010-02-09 14:50:46 +00:00
parent 80df236102
commit 1f4ff1c9d6
8 changed files with 407 additions and 0 deletions

View file

@ -0,0 +1,159 @@
#include "GoogleMaps.h"
#include <CtrlLib/CtrlLib.h>
#include <Web/Web.h>
#include <Geom/Coords/GeomCoords.h>
using namespace Upp;
#define LLOG(x) // LOG(x)
String apikey = "";
void SetGoogleMapsKey(const char *key)
{
apikey = key;
}
String GetGoogleMap(double center_x, double center_y, int zoom, int cx, int cy,
const char *format, String *error)
{
String request;
request << "http://maps.google.com/maps/api/staticmap?center=" <<
AsString(center_y) << ',' << AsString(center_x) <<
"&zoom=" << zoom <<
"&size=" << cx << 'x' << cy <<
"&format=" << format <<
"&sensor=false&key=" << apikey;
return HttpClientGet(request, NULL, error);
}
Image GetGoogleMapImage(double center_x, double center_y, int zoom, int cx, int cy,
const char *format, String *error)
{
return StreamRaster::LoadStringAny(GetGoogleMap(center_x, center_y, zoom, cx, cy, format, error));
}
double CvDeg(double deg, double minutes, double seconds)
{
return deg + (double)minutes / 60 + seconds / 3600;
}
static void sFetch(double& a, double *x, int& ii)
{
a = CvDeg(x[0], x[1], x[2]);
ii = 0;
}
int CharFilterH(int c)
{
return c == 'e' || c == 'E' ? 'r' : c == '\'' || c == '\"' ? '@' : c;
}
Pointf ScanGPS(const char *s)
{
String h = Filter(s, CharFilterH);
Pointf r = Null;
double x[3];
CParser p(h);
x[0] = x[1] = x[2] = 0;
int ii = 0;
while(!p.IsEof()) {
if(p.IsDouble()) {
if(ii < 3)
x[ii++] = p.ReadDouble();
else {
if(IsNull(r.y))
sFetch(r.y, x, ii);
else {
sFetch(r.x, x, ii);
break;
}
x[ii++] = p.ReadDouble();
}
}
else
if(p.Char('W') || p.Char('w')) {
if(x[0] > 0)
x[0] = -x[0];
sFetch(r.x, x, ii);
}
else
if(p.Char('r'))
sFetch(r.x, x, ii);
else
if(p.Char('S') || p.Char('s')) {
if(x[0] > 0)
x[0] = -x[0];
sFetch(r.y, x, ii);
}
else
if(p.Char('N') || p.Char('n'))
sFetch(r.y, x, ii);
else
p.SkipTerm();
}
if(!IsNull(r.y) && IsNull(r.x))
sFetch(r.x, x, ii);
return r;
}
String FormatGPSX(double x)
{
return IsNull(x) ? String() : FormatDegree(x, 2) + (x < 0 ? "W" : "E");
}
String FormatGPSY(double y)
{
return IsNull(y) ? String() : FormatDegree(y, 2) + (y < 0 ? "S" : "N");
}
String FormatGPS(Pointf p)
{
return FormatGPSY(p.y) + ' ' + FormatGPSX(p.x);
}
static const double sOffset = 268435456;
static const double sRadius = sOffset / M_PI;
static int LToX_(double x)
{
return int(sOffset + sRadius * x * M_PI / 180);
}
static int LToY_(double y)
{
return int(sOffset - sRadius * log((1 + sin(y * M_PI / 180))/(1 - sin( y * M_PI / 180))) / 2);
}
static double XToL_(int x)
{
return ((x - sOffset) / sRadius) * 180 / M_PI;
}
static double YToL_(int y)
{
return (M_PI / 2 - 2 * atan(exp((y - sOffset) / sRadius))) * 180 / M_PI;
}
Pointf GoogleMapsPixelToGps(Pointf center, int zoom, Point diff)
{
return Pointf(XToL_(LToX_(center.x) + (diff.x << (21 - zoom))),
YToL_(LToY_(center.y) + (diff.y << (21 - zoom))));
}
Pointf GoogleMapsPixelToGps(Pointf center, int zoom, Size sz, Point p)
{
return GoogleMapsPixelToGps(center, zoom, p - sz / 2);
}
Pointf GoogleMapsGpsToPixelDiff(Pointf center, int zoom, Pointf gps)
{
return Pointf((LToX_(center.x) - LToX_(gps.x)) >> (21 - zoom),
(LToY_(center.y) - LToY_(gps.y)) >> (21 - zoom));
}
Pointf GoogleMapsGpsToPixel(Pointf center, int zoom, Size sz, Pointf gps)
{
return sz / 2 - GoogleMapsGpsToPixelDiff(center, zoom, gps);
}

View file

@ -0,0 +1,35 @@
#ifndef _GoogleMaps_GoogleMaps_h
#define _GoogleMaps_GoogleMaps_h
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
#define LAYOUTFILE <GoogleMaps/GoogleMaps.lay>
#include <CtrlCore/lay.h>
#define IMAGECLASS GoogleMapsImg
#define IMAGEFILE <GoogleMaps/GoogleMaps.iml>
#include <Draw/iml_header.h>
void SetGoogleMapsKey(const char *key);
String GetGoogleMap(double center_x, double center_y, int zoom, int cx, int cy,
const char *format = "png", String *error = NULL);
Image GetGoogleMapImage(double center_x, double center_y, int zoom, int cx, int cy,
const char *format = "png", String *error = NULL);
double CvDeg(double deg, double minutes, double seconds);
Pointf GoogleMapsPixelToGps(Pointf center, int zoom, Point diff);
Pointf GoogleMapsPixelToGps(Pointf center, int zoom, Size sz, Point p);
Pointf GoogleMapsGpsToPixelDiff(Pointf center, int zoom, Pointf gps);
Pointf GoogleMapsGpsToPixel(Pointf center, int zoom, Size sz, Pointf gps);
Pointf ScanGPS(const char *s);
String FormatGPSX(double x);
String FormatGPSY(double y);
String FormatGPS(Pointf p);
bool MapDlg(Pointf& p);
#endif

View file

@ -0,0 +1,31 @@
PREMULTIPLIED
IMAGE_ID(Pin)
IMAGE_BEGIN_DATA
IMAGE_DATA(120,156,173,149,221,43,60,97,20,199,199,122,105,215,187,236,74,12,191,197,134,164,216,245,182,94,195,141,205,75,27,218)
IMAGE_DATA(27,114,129,80,68,45,165,85,36,132,200,80,100,47,182,20,202,205,202,157,43,55,238,246,15,80,254,129,41,127,199,247)
IMAGE_DATA(247,156,211,174,166,105,236,172,151,83,79,187,205,204,243,153,243,253,158,115,158,145,236,146,83,178,73,255,36,147,240,90)
IMAGE_DATA(44,22,197,106,181,198,10,11,11,81,80,80,128,172,172,172,104,90,90,90,80,220,147,205,54,107,35,61,61,61,90,92)
IMAGE_DATA(92,140,250,250,122,244,244,244,96,120,120,24,67,67,67,104,111,111,71,85,85,21,242,242,242,32,30,11,164,128,146,51)
IMAGE_DATA(51,51,213,178,178,50,116,116,116,32,16,8,96,115,115,19,23,23,23,56,57,57,193,242,242,50,70,70,70,208,216,216)
IMAGE_DATA(136,162,162,34,136,252,163,38,121,197,74,74,74,224,241,120,48,57,57,137,173,173,45,60,60,60,224,227,227,3,111,111)
IMAGE_DATA(111,184,190,190,198,202,202,10,124,62,31,231,78,30,136,109,193,47,112,193,220,220,92,212,212,212,96,112,112,16,139,139)
IMAGE_DATA(139,56,61,61,197,243,243,51,84,85,197,251,251,59,179,119,119,119,49,53,53,197,249,147,14,225,41,12,252,148,69,110)
IMAGE_DATA(42,121,214,208,208,192,126,173,174,174,226,252,252,28,143,143,143,120,125,125,197,203,203,11,238,239,239,113,112,112,128,185)
IMAGE_DATA(185,57,244,245,245,161,186,186,154,189,20,53,210,235,86,232,61,14,135,131,189,33,143,214,214,214,56,191,187,187,59,60)
IMAGE_DATA(61,61,49,55,18,137,96,127,127,31,243,243,243,232,239,239,103,45,164,89,228,66,57,122,19,185,137,5,61,143,188,63)
IMAGE_DATA(60,60,68,56,28,198,237,237,45,110,110,110,112,121,121,137,237,237,109,204,206,206,26,241,98,113,94,64,207,163,222,88)
IMAGE_DATA(88,88,224,189,103,103,103,184,186,186,98,214,241,241,49,215,123,122,122,154,245,234,120,136,243,162,90,30,249,71,245,32)
IMAGE_DATA(207,215,215,215,177,183,183,199,156,163,163,35,236,236,236,112,125,39,38,38,208,217,217,105,196,11,196,243,100,158,221,110)
IMAGE_DATA(71,93,93,29,186,186,186,224,247,251,217,167,141,141,13,132,66,33,94,228,233,204,204,12,231,223,210,210,2,167,211,169)
IMAGE_DATA(231,5,227,191,204,163,250,82,205,220,110,55,6,6,6,184,7,201,171,165,165,37,238,31,98,141,141,141,241,204,144,47)
IMAGE_DATA(21,21,21,200,207,207,215,242,162,90,30,205,169,44,203,220,171,173,173,173,236,249,232,232,40,235,27,31,31,231,62,238)
IMAGE_DATA(237,237,69,115,115,51,92,46,23,74,75,75,185,95,140,120,98,206,248,30,205,71,101,101,37,106,107,107,209,212,212,132)
IMAGE_DATA(182,182,54,116,119,119,179,95,244,14,202,139,88,229,229,229,172,39,39,39,199,144,151,145,145,129,236,236,108,246,131,234)
IMAGE_DATA(66,189,79,92,154,127,242,128,22,249,69,26,41,47,98,209,251,197,249,163,231,49,147,174,145,102,155,205,6,154,59,242)
IMAGE_DATA(229,171,69,28,202,139,88,164,75,156,11,218,250,42,244,95,204,12,95,39,238,119,22,237,161,189,113,158,55,209,207,127)
IMAGE_DATA(180,18,161,254,1,75,209,240,130,127,192,211,159,89,177,95,176,140,206,84,239,15,89,170,1,43,17,202,15,120,201,190)
IMAGE_DATA(75,178,244,189,218,36,253,30,197,35,213,254,81,165,212,191,195,159,115,248,67,157,250,48,211,157,138,78,125,124,165,59)
IMAGE_DATA(89,61,205,194,168,222,222,164,59,204,67,171,91,49,121,54,149,72,244,249,111,116,234,67,145,82,212,249,31,15,143,80)
IMAGE_DATA(58,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
IMAGE_END_DATA(832, 1)

View file

@ -0,0 +1,15 @@
LAYOUT(MapDlgLayout, 428, 340)
UNTYPED(map, HSizePosZ(8, 136).VSizePosZ(8, 8))
ITEM(Button, zoomout, SetLabel(t_("-")).RightPosZ(104, 24).TopPosZ(8, 20))
ITEM(DropList, zoom, RightPosZ(40, 60).TopPosZ(8, 19))
ITEM(Button, zoomin, SetLabel(t_("+")).RightPosZ(8, 24).TopPosZ(8, 20))
ITEM(Button, up, RightPosZ(56, 24).TopPosZ(40, 40))
ITEM(Button, left, RightPosZ(88, 40).TopPosZ(80, 24))
ITEM(Button, right, RightPosZ(8, 40).TopPosZ(80, 24))
ITEM(Button, down, RightPosZ(56, 24).TopPosZ(104, 40))
ITEM(Button, cancel, SetLabel(t_("Cancel")).RightPosZ(72, 56).BottomPosZ(8, 24))
ITEM(Button, ok, SetLabel(t_("OK")).RightPosZ(8, 56).BottomPosZ(8, 24))
ITEM(Label, gpsy, SetFrame(BlackFrame()).RightPosZ(8, 120).TopPosZ(164, 24))
ITEM(Label, gpsx, SetFrame(BlackFrame()).RightPosZ(8, 120).TopPosZ(192, 24))
END_LAYOUT

View file

@ -0,0 +1,15 @@
uses
Geom\Coords,
CtrlLib;
file
GoogleMaps.h,
GoogleMaps.cpp,
MapDlg.cpp,
GoogleMaps.lay,
GoogleMaps.iml,
Main.cpp;
mainconfig
"" = "GUI";

View file

@ -0,0 +1,11 @@
#include "GoogleMaps.h"
GUI_APP_MAIN {
// Following key was obtained for U++ project, please get your owen key if you want to use
// GoogleMaps API here (it is free): http://code.google.com/intl/cs/apis/maps/signup.html
SetGoogleMapsKey("ABQIAAAAXHmSFgmVIbMZDJ5RhfPINBSBetkRueiarolywVmVT7jJWZRGvBQFIvrtty50ivBLt4YApjpEVXW8Hw");
Pointf p(0, 51.477222);
if(MapDlg(p))
Exclamation("Marker positions " + FormatGPS(p));
}

View file

@ -0,0 +1,136 @@
#include "GoogleMaps.h"
#define IMAGECLASS GoogleMapsImg
#define IMAGEFILE <GoogleMaps/GoogleMaps.iml>
#include <Draw/iml_source.h>
struct MapImage : public Ctrl {
Image map;
String error;
Point home;
Callback1<Point> WhenLeftClick;
virtual void LeftDown(Point p, dword)
{
WhenLeftClick(p);
}
virtual void Paint(Draw& w) {
Size sz = GetSize();
w.DrawRect(sz, SColorPaper());
if(IsNull(map))
w.DrawText(0, 0, error);
else {
w.DrawImage(0, 0, map);
Point p = GoogleMapsImg::Pin().GetHotSpot();
w.DrawImage(home.x - p.x, home.y - p.y, GoogleMapsImg::Pin());
}
}
MapImage() { SetFrame(ViewFrame()); BackPaint(); }
};
struct MapDlgDlg : public WithMapDlgLayout<TopWindow> {
typedef MapDlgDlg CLASSNAME;
Pointf home;
Pointf center;
MapImage map;
void LoadMap();
void SetHome();
void MapClick(Point p);
void ZoomIn();
void ZoomOut();
void Move(int x, int y);
void Set(Pointf p);
Pointf Get() { return home; }
MapDlgDlg();
};
void MapDlgDlg::SetHome()
{
map.home = GoogleMapsGpsToPixel(center, ~zoom, Size(640, 640), home);
gpsy.SetLabel(" " + FormatGPSY(home.y));
gpsx.SetLabel(" " + FormatGPSX(home.x));
map.Refresh();
}
void MapDlgDlg::LoadMap()
{
map.map = GetGoogleMapImage(center.x, center.y, ~zoom, 640, 640, "png", &map.error);
SetHome();
}
void MapDlgDlg::Move(int x, int y)
{
center = GoogleMapsPixelToGps(center, ~zoom, Point(250 * x, 250 * y));
LoadMap();
}
void MapDlgDlg::MapClick(Point p)
{
home = GoogleMapsPixelToGps(center, ~zoom, Size(640, 640), p);
SetHome();
}
void MapDlgDlg::Set(Pointf p)
{
home = center = p;
LoadMap();
}
void MapDlgDlg::ZoomIn()
{
zoom <<= min((int)~zoom + 1, 21);
LoadMap();
}
void MapDlgDlg::ZoomOut()
{
zoom <<= max((int)~zoom - 1, 0);
LoadMap();
}
MapDlgDlg::MapDlgDlg()
{
CtrlLayoutOKCancel(*this, "");
Size sz = GetSize();
Size msz = map.GetSize();
sz += Size(640 - msz.cx, 640 - msz.cy);
SetRect(sz);
for(int i = 0; i < 22; i++)
zoom.Add(i);
zoom <<= 17;
zoom <<= THISBACK(LoadMap);
zoomin <<= THISBACK(ZoomIn);
zoomout <<= THISBACK(ZoomOut);
left <<= THISBACK2(Move, -1, 0);
left.SetImage(CtrlImg::SmallLeft());
right <<= THISBACK2(Move, 1, 0);
right.SetImage(CtrlImg::SmallRight());
up <<= THISBACK2(Move, 0, -1);
up.SetImage(CtrlImg::SmallUp());
down <<= THISBACK2(Move, 0, 1);
down.SetImage(CtrlImg::SmallDown());
map.WhenLeftClick = THISBACK(MapClick);
}
bool MapDlg(Pointf& p)
{
MapDlgDlg dlg;
dlg.Set(p);
if(dlg.Run() == IDOK) {
p = dlg.Get();
return true;
}
return false;
}

5
examples/GoogleMaps/init Normal file
View file

@ -0,0 +1,5 @@
#ifndef _GoogleMaps_icpp_init_stub
#define _GoogleMaps_icpp_init_stub
#include "Geom\Coords/init"
#include "CtrlLib/init"
#endif