champs
champs

Reputation: 59

Google Maps Api Integration in Win32 C/C++ Desktop Application

We want to integrate Google maps into our C/C++ win32 application. The whole idea is when user runs the application for the first time we request them to mark their location and then retrieve the coordinates of the user selected location and save into a database or file.

After it has been initialized we want to provide user with some pre tagged places and allow user to find direction from their place to that place. The coordinates of the pre tagged locations will be provided.

Is it possible to achieve what we want by using Google Api's, if yes then how?? And if there are legal issues regarding it??

Thanks.

Upvotes: 3

Views: 6919

Answers (3)

Michael Haephrati
Michael Haephrati

Reputation: 4235

I needed the same and solved it by interfacing Google Maps via a Browser object.

*** Please read about WebBrowser control compatibility and follow the instructions.

  1. Create an MFC dialog based project.

  2. Add a CExplorer control. (See: https://msdn.microsoft.com/en-us/library/aa752046(v=vs.85).aspx).

  3. When the main dialog of your program initializes, you need to initialize a global object

     CExplorer1 m_Browser;
    
  4. The first building block would be WriteHTML()

    void WriteHTML(const wchar_t* html)
    
    {
    
       IDispatch* pHtmlDoc = m_Browser.get_Document();
    
       if (!pHtmlDoc)
    
           return;
    
       CComPtr<IHTMLDocument2> doc1 = NULL;
    
       doc1.Detach();
    
       doc1.Attach((IHTMLDocument2*)pHtmlDoc);
    
       if (!doc1)
    
           return;
    
       // Creates a new one-dimensional array
    
       SAFEARRAY* psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
    
       if (!psaStrings)
    
           return;
    
       BSTR bstr = SysAllocString(html);
    
       if (bstr)
    
       {
    
           VARIANT* param;
    
           HRESULT hr = SafeArrayAccessData(psaStrings, (LPVOID*)&param);
    
           if (SUCCEEDED(hr))
    
           {
    
               param->vt = VT_BSTR;
    
               param->bstrVal = bstr;
    
               hr = SafeArrayUnaccessData(psaStrings);
    
               if (SUCCEEDED(hr))
    
               {
    
                   doc1->write(psaStrings);
    
                   doc1->close();
    
               }
    
           }
    
       }
    
       // SafeArrayDestroy calls SysFreeString for each BSTR!
    
       if (psaStrings)
    
           SafeArrayDestroy(psaStrings);
     }
    
  5. You should also add the browser control to your .rs file

    CONTROL "",IDC_SGWEBBROWSER,"{8856F961-340A-11D0-A96B- 00C04FD705A2}",WS_TABSTOP,388,36,236,281

  6. Among several ways to implement it, I found that creating a temporary HTML based on the coordinates and calling: m_Browser.Navigate(FileName, 0, 0, 0, 0); is the best.

  7. "FileName" is created based on the requested coordinates of the point on the map, along with other attributes as follow:

  8. Assign a name and path to "FileName":

      wchar_t FileName[2048];
    
      GetCurrentDirectory(2048, FileName);
    
      wcscat(FileName, L"\\test.html");
    
  9. Filling the file with data and opening it with the browser control, whilst navigating Google Maps to the requested location:

Variables:

a. Latitude

b. Longitude

c. zoom (set to 10)

d. API key. (you need to obtain one)

Code (an alternative to the WriteHTML function)

CString HTML_TEXT;
CRect rect;
CWnd *pWnd = GetDlgItem(IDC_SGWEBBROWSER);
pWnd->GetWindowRect(&rect);
int w = rect.Width()-50, h = rect.Height()-50;
HTML_TEXT.Format(L"<!DOCTYPE html><html><meta http-equiv=\"X - UA - Compatible\" content=\"IE = edge\"><body><div id =\"googleMap\" style=\"width:%dpx;height:%dpx\"><script>function myMap(){var mapProp = {center:new google.maps.LatLng(%f, %f), zoom : 10};var map = new google.maps.Map(document.getElementById(\"googleMap\"), mapProp);marker = new google.maps.Marker({position: new google.maps.LatLng(%f, %f),map: map});}</script><script src = \"https://maps.googleapis.com/maps/api/js?key=%s&callback=myMap\"></script></div></body></html>", w, h, Latitude, Longitude, Latitude, Longitude, API_KEY);
FILE *fp = _wfopen(FileName, L"w");
fwprintf(fp, L"%s", HTML_TEXT.GetBuffer());
fclose(fp);
m_Browser.Navigate(FileName, 0, 0, 0, 0);

Upvotes: 0

s4eed
s4eed

Reputation: 7891

You can use Qt framework on Windows. You can integrate your win32 application with Qt. In Qt you may use QWebKit framework to communicate with your app and a webpage(which, in this scenario is a page containing a map from Google Maps) . Actually you can write hybrid applications with Qt. Take a look at Qt QWebKit documentation.

Upvotes: 0

Brandin
Brandin

Reputation: 916

For the answer to the legal question, the best place is to look at the Terms of Service, and then to consult a lawyer of course. Both terms of service for the Maps API as well as the Maps for Business Terms of Service explicitly prohibit the usage of geocoding data if it is not in conjunction with a Google Map:

Maps API TOS, relevant point is Section 10.1.1 (g): https://developers.google.com/maps/terms#section_10_1

Business Maps Purchase Agreement, relevant point is 4.1 (l): https://www.google.com/enterprise/earthmaps/legal/us/maps_purchase_agreement.html

If you need a more liberal alternative you might want to look at http://www.openstreetmap.org/ - the geocoding data offered there is covered under the Open Database License, which permits usage with attribution.

http://wiki.openstreetmap.org/wiki/Legal_FAQ

Upvotes: 1

Related Questions