Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » UppHub » Using GoogleMaps from U++
Using GoogleMaps from U++ [message #24774] Fri, 29 January 2010 16:23 Go to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Not sure where to put this, but I need it for my work and I guess it would be interesting for others:

#include <CtrlLib/CtrlLib.h>
#include <Web/Web.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 = "png", String *error = NULL)
{
	String request;
	request << "http://maps.google.com/maps/api/staticmap?center=" <<
		       AsString(center_x) << ',' << AsString(center_y) <<
		       "&zoom=" << zoom <<
		       "&size=" << cx << 'x' << cy <<
		       "&format=" << format <<
		       "&sensor=false&key=" << apikey;
	LLOG(request);
	return HttpClientGet(request, NULL, error);
}

Image GetGoogleMapImage(double center_x, double center_y, int zoom, int cx, int cy,
                    const char *format = "png", String *error = NULL)
{
	return StreamRaster::LoadStringAny(GetGoogleMap(center_x, center_y, zoom, cx, cy, format, error));
}

struct App : public TopWindow {
	virtual void Paint(Draw& w) {
		Size sz = GetSize();
		String error;
		w.DrawRect(sz, SColorPaper());
		Image m = GetGoogleMapImage(40.714728, -73.998672, 15, 640, 640, "png", &error);
		if(IsNull(m))
			w.DrawText(0, 0, error);
		else
			w.DrawImage(0, 0, m);
	}
};

GUI_APP_MAIN
{
	SetGoogleMapsKey(/* put your API key here */);
	
	App().Run();
}
Re: Using GoogleMaps from U++ [message #24778 is a reply to message #24774] Fri, 29 January 2010 17:49 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Nice!
index.php?t=getfile&id=2201&private=0

Thank you Mirek!

Regards,
Ion Lupascu (tojocky)

luzr wrote on Fri, 29 January 2010 17:23

Not sure where to put this, but I need it for my work and I guess it would be interesting for others:

#include <CtrlLib/CtrlLib.h>
#include <Web/Web.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 = "png", String *error = NULL)
{
	String request;
	request << "http://maps.google.com/maps/api/staticmap?center=" <<
		       AsString(center_x) << ',' << AsString(center_y) <<
		       "&zoom=" << zoom <<
		       "&size=" << cx << 'x' << cy <<
		       "&format=" << format <<
		       "&sensor=false&key=" << apikey;
	LLOG(request);
	return HttpClientGet(request, NULL, error);
}

Image GetGoogleMapImage(double center_x, double center_y, int zoom, int cx, int cy,
                    const char *format = "png", String *error = NULL)
{
	return StreamRaster::LoadStringAny(GetGoogleMap(center_x, center_y, zoom, cx, cy, format, error));
}

struct App : public TopWindow {
	virtual void Paint(Draw& w) {
		Size sz = GetSize();
		String error;
		w.DrawRect(sz, SColorPaper());
		Image m = GetGoogleMapImage(40.714728, -73.998672, 15, 640, 640, "png", &error);
		if(IsNull(m))
			w.DrawText(0, 0, error);
		else
			w.DrawImage(0, 0, m);
	}
};

GUI_APP_MAIN
{
	SetGoogleMapsKey(/* put your API key here */);
	
	App().Run();
}


icon14.gif  Re: Using GoogleMaps from U++ [message #24785 is a reply to message #24778] Fri, 29 January 2010 20:38 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Fantastic,

Just what I needed !!! Very Happy

Thanks
Re: Using GoogleMaps from U++ [message #24904 is a reply to message #24785] Wed, 03 February 2010 18:30 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Didier wrote on Fri, 29 January 2010 14:38

Fantastic,

Just what I needed !!! Very Happy

Thanks


Now with latitude/longitude <-> pixel conversions:

#include <CtrlLib/CtrlLib.h>
#include <Web/Web.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 = "png", String *error = NULL)
{
	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;
	LLOG(request);
	return HttpClientGet(request, NULL, error);
}

Image GetGoogleMapImage(double center_x, double center_y, int zoom, int cx, int cy,
                    const char *format = "png", String *error = NULL)
{
	return StreamRaster::LoadStringAny(GetGoogleMap(center_x, center_y, zoom, cx, cy, format, error));
}

double CvDeg(int deg, int minutes, double seconds)
{
	return deg + (double)minutes / 60 + seconds / 3600;
}

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);
}

struct App : public TopWindow {
	Pointf home;
	Pointf center;
	String error;
	Image  map;
	int    zoom;
	
	virtual void LeftDown(Point p, dword)
	{
		center = GoogleMapsPixelToGps(center, zoom, Size(640, 640), p);
		map = GetGoogleMapImage(center.x, center.y, zoom, 640, 640, "png", &error);
		Refresh();
	}
	
	virtual void Paint(Draw& w) {
		Size sz = GetSize();
		String error;
		w.DrawRect(sz, SColorPaper());
		if(IsNull(map))
			w.DrawText(0, 0, error);
		else {
			w.DrawImage(0, 0, map);
			w.DrawImage(320, 320, CtrlImg::arrow());
			Point hp = GoogleMapsGpsToPixel(center, zoom, Size(640, 640), home);
			w.DrawImage(hp.x, hp.y, CtrlImg::arrow());
		}
	}

	App() {
		zoom = 15;
		home = center = Pointf(CvDeg(14, 22, 21.75), CvDeg(50, 7, 57.96));
		map = GetGoogleMapImage(center.x, center.y, zoom, 640, 640, "png", &error);
	}
};

GUI_APP_MAIN
{
	SetGoogleMapsKey(....);
	
	App().Run();
}


Re: Using GoogleMaps from U++ [message #25061 is a reply to message #24904] Tue, 09 February 2010 13:07 Go to previous messageGo to next message
alex100 is currently offline  alex100
Messages: 118
Registered: November 2007
Experienced Member
When I try this code compiler says:

C:\upp\MyApps\App1\main.cpp: In function 'Upp::Pointf GoogleMapsGpsToPixel(Upp::Pointf, int, Upp::Size, Upp::Pointf)':

C:\upp\MyApps\App1\main.cpp:82: error: ambiguous overload for 'operator-' in 'Upp::operator/(Upp::Size_<int>, int)(2) - Google
MapsGpsToPixelDiff(Upp::Pointf, int, Upp::Pointf)(zoom, Upp::Point_<double>(((const Upp::Point_<double>&)((const Upp::Point_
<double>*)(& gps)))))'

Please, could some one help me fixing this?

Thanks a lot

Alex
Re: Using GoogleMaps from U++ [message #25063 is a reply to message #25061] Tue, 09 February 2010 13:55 Go to previous messageGo to next message
chickenk is currently offline  chickenk
Messages: 169
Registered: May 2007
Location: Grenoble, France
Experienced Member
Here is what I did:
Pointf GoogleMapsGpsToPixel(Pointf center, int zoom, Size sz, Pointf gps)
{
	return ((Sizef)sz) / 2.0 - GoogleMapsGpsToPixelDiff(center, zoom, gps);
}

regards,
Lionel
Re: Using GoogleMaps from U++ [message #27582 is a reply to message #25063] Sat, 24 July 2010 10:00 Go to previous message
alex100 is currently offline  alex100
Messages: 118
Registered: November 2007
Experienced Member
By the way, can we use googlemaps for commercial applications? No right?
Can we use googlemaps for desktop applications? No right?

Can we use googlemaps in a private/access controled web page? No right?

Can we use google maps in a web page with more than xxx hits/requests a month. No right?

All this is true, right?

[Updated on: Sat, 24 July 2010 12:02]

Report message to a moderator

Previous Topic: Docking: ContexMenu behaviour
Next Topic: NEW: PortQueue (M$ CCR port to u++)
Goto Forum:
  


Current Time: Fri Apr 19 06:53:56 CEST 2024

Total time taken to generate the page: 1.02286 seconds