Geolocation: Google Maps

Developed or developing a new product in PureBasic? Tell the world about it.
Little_man
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Mar 29, 2013 4:55 pm
Location: The Netherland

Re: Geolocation via WebGadget

Post by Little_man »

Hi JHPJHP,

Using a modified version of the quick method from the first post, directions can be returned in either an XML or JSON format:

Code: Select all

InitNetwork()
Debug PeekS(ReceiveHTTPMemory("http://maps.google.com/maps/api/directions/xml?origin=Toronto&destination=Mount+Rushmore"), -1, #PB_UTF8)

Code: Select all

InitNetwork()

Dim output.s(0) ;this will be resized later

Procedure ExplodeStringArray(Array A.s(1), S.s, Delimeter.s)
  Protected Count.i, I.i
  Count.i = CountString(S.s, Delimeter.s) + 1
  
  ;Debug Str(count) + " substrings found"
  Dim A.s(Count.i)
  For I.i = 1 To Count.i
    A.s(I.i - 1) = StringField(S.s, I.i, Delimeter.s)
  Next
  ProcedureReturn Count.i ;Return count of substrings
EndProcedure

Text.s = PeekS(ReceiveHTTPMemory("http://maps.google.com/maps/api/directions/xml?origin=oslo&destination=gibraltar&&language=en"), -1, #PB_UTF8)
(language=en --> "en" = own language)

ExplodeStringArray(Output(), Text.s, #LF$)

Debug "Travel directions" + ":"
Debug "=================== "

Teller.i = 1
For I.i = 0 To ArraySize(Output())
  If FindString(Output(I.i), "<html_instructions>", 1) > 0
    Output(I.i) = RemoveString(Output(I.i), "<html_instructions>")
    Output(I.i) = RemoveString(Output(I.i), "</html_instructions>")
    Output(I.i) = RemoveString(Output(I.i), "    ")
    Output(I.i) = RemoveString(Output(I.i), ";b>")
    Output(I.i) = RemoveString(Output(I.i), "&lt")
    Output(I.i) = RemoveString(Output(I.i), ";/b>")
    Output(I.i) = RemoveString(Output(I.i), ";/div>")
    Output(I.i) = RemoveString(Output(I.i), ";div style="font size:0.9em">")
    Output(I.i) = RemoveString(Output(I.i), ";div style="font-size:0.9em">")
    
    Debug RSet(Str(Teller.i), 3, "0") + " -  " + Output(I.i)
    Teller.i + 1
  EndIf
Next I.i

Debug ""
Debug "End travel directions" + ":"
Debug "========================="
Little_man
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation via WebGadget

Post by JHPJHP »

Hi Little_man,

That's very useful script, well done.

------------------------------------------------------------

Updated:
- added 1 example
-- Geolocation_Side_by_Side.pb / Geolocation_Side_by_Side_OSX.pb (geolocation_side_by_side.pbi)
-- display a map location beside the Street View of the same location
-- drag the golden emoji on the map side to a new location
-- click the size icon on the Street View side to toggle full-screen
Last edited by JHPJHP on Thu May 30, 2019 4:58 am, edited 1 time in total.
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Geolocation via WebGadget

Post by Keya »

this is a very cool thread! and i dont want to take away from what its main ability is, but I cant help but wonder -- the webgadget is getting the geolocation seemingly from the OS somewhere, so im thinking it would be better in at least some cases to also just directly get that data from the OS rather than go through the behemoth webgadget? :) I dont know where the data is coming from though but it's not like the web browser ever asks for it so surely its just existing OS data, along the lines of locale perhaps?
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation via WebGadget

Post by JHPJHP »

Hi Keya,

The information is being requested from the Google Maps service...

See the first post [ Quick Method ] for script on how to request Geolocation data directly without using the WebGadget.

Here Little_man posted script to parse data using the Quick Method.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated:
- added an API Key (max 25000 requests per day)

It seems that Google is now making the key mandatory, so I've added one for testing purposes.

If you are using this method for more then just demoing the code, please get your own key, their free up to 25000 requests per day.
- https://developers.google.com/maps/docu ... ript/usage

-----------------------------------------------------------------------

Updated:
- added a Linux version to the first post (tested in Ubuntu 17.04 :: 64 bit)
- applied some minor bug fixes to all packages

NB*: If you haven't already done so, you will need to install the Linux packages located in Setup/install_files.sh.

-----------------------------------------------------------------------

It has been reported that "Google has completely switched off the API-Key free version of their geocoding"...

While an API key is required, it comes with $200 in free credit per month; see the following: Google Cloud.

To test this I recently removed my billing account from the Google project assigned with the Google Maps API key. When the program was executed it reported that the query limit had been reached.

I then created a new billing account assign to the above mentioned Google project. When the program was executed it worked without error.

NB*: When setting up the billing account it states that no charges will be applied if the credit limit is reached until manually authorized.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Windows only):
- image sizes configured to use the built-in PureBasic DPI compiler option

--------------------------------------------------------

I thought I had all the DPI issues worked out, but further testing revealed numerous problems.

Tested Scale: 100%, 125%, 150%, 175%

Instead of reposting all the updated applications, I will just list them here:
- Geolocation: Google Maps
- Barcode Generator
- Deform Image: MLS
- LSB File Embedding
Last edited by JHPJHP on Mon Jun 03, 2019 10:37 pm, edited 1 time in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Windows only):
- added full screen view; pressing the Google Maps full screen icon will now trigger a PureBasic event

Full Screen View
Applied some of the same techniques developed for HTML5 YouTube Player and HTML5 MP4 Player.

---------------------------------------------------------------

Updated (Windows only):
- added a splash window
- added a control window; full screen view
- enhanced full screen view

Before this version the examples were designed as templates to help others embed Google Maps into an application. With the latest updates the examples became more relevant as individual tools.

NB*: Most of the code has been restructured for improved performance and usability.
Last edited by JHPJHP on Mon Jun 03, 2019 10:36 pm, edited 1 time in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Windows only):
- added interactive markers; dragging a Google Map marker will now trigger a PureBasic event

Interactive Markers
Geolocation_Directions.pb includes two draggable markers, Geolocation_Side_by_Side.pb includes a draggable pegman.

Reference Material: https://developers.google.com/maps/docu ... tionsRoute

------------------------------------------------------------

Updated (Window only):
- squashed some bugs
- better transitions
- some code improvements
- various enhancements
Last edited by JHPJHP on Tue Jun 18, 2019 6:23 am, edited 1 time in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Window only):
- saving map types between views
- renamed events to better match JS names
- cleaned up JS code some-what
- added a couple improvements

Map Types
Map types are now remembered between full screen and normal view.
Google wrote:The following map types are available in the Maps JavaScript API:
roadmap :: displays the default road map view. This is the default map type.
satellite :: displays Google Earth satellite images.
hybrid :: displays a mixture of normal and satellite views.
terrain :: displays a physical map based on terrain information.
Last edited by JHPJHP on Mon Jun 03, 2019 10:36 pm, edited 3 times in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Window only):
- capturing zoom events
- capturing heading & pitch events
- squashed a bug
- optimized some of the JS code

Zoom Events
Zoom events are now passed from JS to PureBasic with the exception of Geolocation_IP_Address.pb.
Panorama zoom events are now supported in the example Geolocation_Side_by_Side.pb.

Heading & Pitch Events
Panorama heading & pitch events are now supported in the example Geolocation_Side_by_Side.pb.
Last edited by JHPJHP on Tue Jun 18, 2019 6:24 am, edited 1 time in total.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Window only):
- added a new option to Geolocation_Directions.pb, Geolocation_Two_Points.pb
- improved event and error handling
- squashed a small bug
- some minor code improvements

Geolocation_Directions.pb
The "fit bounds" function is built-in to this type of map; unchecking Auto Zoom option will override this feature.

Geolocation_Two_Points.pb
Added the "fit bounds" function; unchecking Auto Zoom option will override this feature.

NB*: The next update will include a look-up database of previous searches.
Last edited by JHPJHP on Mon Jun 10, 2019 5:34 pm, edited 2 times in total.
Little_man
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Mar 29, 2013 4:55 pm
Location: The Netherland

Re: Geolocation: Google Maps

Post by Little_man »

JHPJHP,

;-----------------------------------------------------------------------------------------------------------------------------------------------------
Gives an error on your website;

Forbidden

You don't have permission to access / on this server.


Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
;-----------------------------------------------------------------------------------------------------------------------------------------------------

Little_man.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Hi Little_man,

I have temporarily made changes to the websites main .htaccess file; currently only direct access is available.

--------------------------------------------

Updated (Windows):
- all four examples have received numerous improvements
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: Geolocation: Google Maps

Post by JHPJHP »

Updated (Windows):
- added Location List of previously visited sites; some items can be modified directly
-- right-mouse click item to toggle TRUE / FALSE
-- right-mouse click item to open context menu
-- press delete key to delete record
-- click column to change sort order
-- double-click record to open location

Geolocation_Directions.pb
- dragging A / B marker to a new location will create a new record (map object)
- item is updated when Travel Mode is changed (PB gadget)
- item is updated when Hide Panel is changed (PB gadget)
- item is updated when Auto Zoom is changed (PB gadget)
- item is updated when Map Type is changed (map object)
- * item is updated when Full Screen is toggled (map object)
- item is updated when Zoom Level is changed (map object)

Geolocation_IP_Address.pb
- item is updated when Zoom Level is changed (PB gadget)
- item is updated when Map Type is changed (map object)
- * item is updated when Full Screen is toggled (map object)

Geolocation_Side_by_Side.pb
- dragging pegman to a new location will create a new record (map object)
- item is updated when Map Type is changed (map object)
- * item is updated when Full Screen is toggled (map / panorama object)
- item is updated when Split Screen is toggled (map / panorama object)
- item is updated when Zoom Level is changed (map / panorama object)
- item is updated when Heading is changed (panorama object)
- item is updated when Pitch is changed (panorama object)

Geolocation_Two_Points.pb
- item is updated when Auto Zoom is changed (PB gadget)
- item is updated when Map Type is changed (map object)
- * item is updated when Full Screen is toggled (map object)
- item is updated when Zoom Level is changed (map object)

When the program is first opened the location will default to the first saved record.
* Press the Escape key to exit full screen without updating the full screen item.
Double click the Control window title bar for semi-transparent, single click to exit transparency.
The Control window close button will exit the program.
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Geolocation: Google Maps

Post by JHPJHP »

Updated (Windows):
- Geolocation_Directions.pb
-- added styles\geolocation_directions.json
- renamed Geolocation_Side_by_Side.pb to Geolocation_Split_View.pb
- Geolocation_Split_View.pb
-- added includes\geolocation_autocomplete.pbi
- small enhancements and bug fixes to all examples

Geolocation_Directions.pb
Styled MapType added; JSON file created using https://mapstyle.withgoogle.com/.

Geolocation_Split_View.pb
Replaced a PureBasic StringGadget with a Google Maps Places Autocomplete window, designed to look like the original StringGadget.
Due to the design layout of the main window, the Autocomplete window is styled with #WS_EX_LAYERED (steals focus from main window).
With a different design choice the Autocomplete window could have been styled with #WS_CHILD & ~(#WS_POPUP).
Alternately, a WebGadget could have been placed directly on the main window, as was done with Barcode Generator.
Last edited by JHPJHP on Fri Jun 21, 2019 2:50 pm, edited 1 time in total.
Locked