A Simple Geolocation Example to fetch Latitude and Longitude

719 views
Skip to first unread message

TEJASVI N

unread,
Apr 8, 2015, 5:54:47 AM4/8/15
to cefs...@googlegroups.com
Hi,
As Geolocation has been implemented in Cefsharp. Can somebody post a simple example which can retrieve Latitude and longitude in winform / wpf application.

I only see the example of enabling permission set by user to use geolocation at Github site and tried in few ways but I was not able to get Latitude and longitude.

It would be helpful if somebody can guide me or post an simple example.

Thanks in advance !!

Alex Maitland

unread,
Apr 8, 2015, 5:15:51 PM4/8/15
to cefs...@googlegroups.com
What have you tried so far?

I would have though

- Set API keys
- Implement `IGeolocationHandler`
- Request location in Javascript

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf.Example/Handlers/GeolocationHandler.cs
http://www.w3schools.com/html/html5_geolocation.asp

TEJASVI N

unread,
Apr 9, 2015, 2:30:32 AM4/9/15
to cefs...@googlegroups.com
Hi,

What i have tried is
1. Setting up the key
2. Implementing the IGeolocation interface
3. I tried to make location request in JS from HTML5 geolocation api  using cefsharp EvaluateScriptAsync method. Still i was not able to get the location. It threw user denied permission.

I am still doubtful that making location call from javascript would work for desktop applications because HTML5 geolocation api would only work when files are hosted in an server (Example : any http server like node or python server). But here in desktop applications they would be loaded as file protocol for which the api would not work.

http://stackoverflow.com/questions/5423938/html-5-geo-location-prompt-in-chrome

The link above will give few details about geolocation.

The main thing i would want to know is when i implement cefsharp IGeolocation interface will it only set the permission to access the geolocation or will it even provide the lat long as part of call back. There is even a callback which has been written as part of that implementation in cpp code.

Thanks in advance !!

Alex Maitland

unread,
Apr 9, 2015, 3:12:13 AM4/9/15
to cefs...@googlegroups.com
If you look at the PR that implemented CefGeolocationHandler it's purely about permissions.


You'll likely need to implement `CefGetGeolocation`


Quickest way to get this implemented is to submit a PR.

Alex Maitland

unread,
Apr 9, 2015, 4:25:38 AM4/9/15
to cefs...@googlegroups.com
You should be able to use https://github.com/cefsharp/CefSharp/pull/932/files as a basis

TEJASVI N

unread,
Apr 9, 2015, 5:05:18 AM4/9/15
to cefs...@googlegroups.com
Hi,

When you look the IGeolocationHandler interface defined in C# it just takes 3 parameters i.e browser, url and request id. Nowhere in the examples they have talked about the callbacks which is been implemented in CPP file "CefSharp.Core/Internals/ClientAdapter.h".

So still its confusing how to go about it? Any simple snippet in winforms would be helpful.

Alex Maitland

unread,
Apr 9, 2015, 5:11:04 AM4/9/15
to cefs...@googlegroups.com
The callback is just a Allow/Cancel mechanism, it could potentially be implemented in an async fashion. The current implementation is in a sync fashion. The CEF API has the full description.

http://magpcss.org/ceforum/apidocs3/projects/%28default%29/CefGeolocationHandler.html#OnRequestGeolocationPermission%28CefRefPtr%3CCefBrowser%3E,constCefString&,int,CefRefPtr%3CCefGeolocationCallback%3E%29

Read the documentation for `CefGetGeolocation`, it's likely a better fit for what you require.

Alex Maitland

unread,
Apr 9, 2015, 5:12:10 AM4/9/15
to cefs...@googlegroups.com
You maybe better off trying to contact the original PR creator for some advise, they're likely far more experienced in the field.
Message has been deleted

TEJASVI N

unread,
Apr 9, 2015, 5:38:56 AM4/9/15
to cefs...@googlegroups.com
Yes will do that. Thanks for the info again !!

If somebody comes up with a solution more sooner in this forum that would be helpful.

Alex Maitland

unread,
Apr 9, 2015, 6:14:26 AM4/9/15
to cefs...@googlegroups.com
Can I ask, is there any reason your not going to implement `CefGetGeolocation`? To me that sounds like the quickest way to a solution, should take around 15-30mins.

Part of that would be implementing the `CefGetGeolocationCallback` callback which gives you longitude and latitude.

http://magpcss.org/ceforum/apidocs3/projects/%28default%29/_cef_geoposition_t.html

TEJASVI N

unread,
Apr 9, 2015, 7:47:24 AM4/9/15
to cefs...@googlegroups.com
Hi Alex,

I am quite confused with what interfaces I need to implement as i tried to look into the links you pointed out to me.

It would be helpful if you can give a sample snippet so that i can try by looking into it..

Alex Maitland

unread,
Apr 9, 2015, 8:17:53 AM4/9/15
to cefs...@googlegroups.com
If you look at https://github.com/cefsharp/CefSharp/pull/932/files you'll see an example of another feature that follows a similar pattern.

Create a class e.g `GetGeolocationCallback` that implements `CefGetGeolocationCallback`, implement `OnLocationUpdate`, call `SetResult` on the `TaskComplectionSource`, your pretty much done.

I'm unsure if `OnLocationUpdate` is ever called multiple times, if it is then another strategy will need to be used. (Something similar to the `CookieVisitor`, where an interface is used instead).

Code is the best documentation. There are no examples of exactly what your looking for. Start simple and work your way up. It's all pretty easy once you get going.

Rupesh Techfamous

unread,
Feb 1, 2018, 5:35:20 AM2/1/18
to CefSharp
Hi,

I am also failed to get the permission for location access.
Every step tried. Implemented IGeolocationHandler interface and set all API keys from Google.
How to proceed? 
Reply all
Reply to author
Forward
0 new messages