Android Question Change WebView UserAgent

agraham

Expert
Licensed User
Longtime User
I'm no good at webby stuff - I have a longstanding mental block about it. I want to display Bing maps in a WebView but I want it to have a desktop useragent. No Google Maps will not do because only Bing maps has online access to the jealously guarded Ordnance Survey 50K and 25K mapping for the UK. However these are only allowed for a desktop browser. I can see the OS maps in Edge on the device as Edge has an option to switch to desktop mode.

I can open Bing maps in a WebView OK but I assume need a useragent change to put it into desktop mode. Any ideas as to how I could do this and what should the useragent string be to simulate a desktop browser.
 

JohnC

Expert
Licensed User
Longtime User
Here is the code to change it:
B4X:
Dim wvs As WebViewSettings
'initialize wvs here
'wv = webview

Dim UA As String
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246" 'or set any of the useragents listed below
wvs.setUserAgentString(wv,UA)

Here are some common useragents:

 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
see how that goes. there are other options.
 

Attachments

  • os1.png
    os1.png
    179 KB · Views: 182
  • os2.png
    os2.png
    120.2 KB · Views: 174
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've got a WebView displaying the desktop version of Bing maps but the UI is not behaving properly, probably because the WebView is not a full browser :(
@drgottjr how did you get to display your os2.png image? It is not however the OS 50K mapping which has much more detail. This is a clip from Bing Maps on my desktop of the same area.
 

Attachments

  • 2023-02-27_135840.jpg
    2023-02-27_135840.jpg
    173.2 KB · Views: 162
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Try adding "ChromeClient" to the webview using WebViewExtras:

B4X:
Dim wve as WebViewExtras

wve.addWebChromeClient(wv,  "WVE")    'wve 2
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Try adding "ChromeClient" to the webview using WebViewExtras:
Unfortunately the UI is still messed up. Maybe touch doesn't translate very well to the mouse movements that the page is expecting, but the drop down option to select the map type doesn't work properly either. And the advertising side bar won't stay hidden. Maybe there is JavaScript that isn't executing properly.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Then maybe try this other version of Webview:

 
Upvote 0

agraham

Expert
Licensed User
Longtime User
UltimateWebView seems not to be there for download. The its successor consists of stuff that looks utterly alien to me. After adding all those WeKit... libraries I've got it to compile and run a WebKitView but I'm back needing to change the user agent and I can't see how. I think I'll give up. This web stuff is above my pay grade :(

EDIT: Adding the libraries seems to break B4X Object Browser :(
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
I've got a WebView displaying the desktop version of Bing maps but the UI is not behaving properly, probably because the WebView is not a full browser :(
@drgottjr how did you get to display your os2.png image? It is not however the OS 50K mapping which has much more detail. This is a clip from Bing Maps on my desktop of the same area.
just fyi, you don't need edge to see bing's os maps. but mobile does not appear to be supported. os data are available at osdatahub.os.uk. there is an allegedly fully working example for you to copy and paste. i don't use it, but it probably works (i'm familiar with the particular framework used for the example). in any case, i just pointed my app at the appropriate endpoint and got the rendered tiles the same as from any other source. 50K, 25K - i care not a jot (as some would say). whatever you're looking for should be there. bing's presence in the matter is a little unclear. i was curious as to why os's own renderings looked nothing like bing's garish maps. os maps (or, at least, os-style maps) used to be available about 10 years ago through a company that seems to have disappeared. if i'm not mistaken, microsoft may have bought them out and acquired the rights to distribute os maps (while allowing os itself the right to serve tiles from its own site. to see the renderings, i had to tell the internet that i was in the uk as os maps are not available from bing if one's ip address is not in the uk.)

i'm pretty sure you can accumulate a nice personal collection of os tiles for offline use. i looked at the site rather quickly and solely in response to your query; there seemed to be a lot there for os-ophiles. you can pm if you have slippy-related queries.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
you don't need edge to see bing's os maps. but mobile does not appear to be supported.
The site detects mobile browsers and won't serve them the 50K and 25K OS maps. That's why the user agent needs to be changed. Firefox and Chrome on my devices won't show the site at all in either mobile or desktop mod.. Only Edge seems to work, both in mobile and desktop mode.

os maps (or, at least, os-style maps) used to be available about 10 years ago through a company that seems to have disappeared. if i'm not mistaken, microsoft may have bought them out and acquired the rights to distribute os maps (while allowing os itself the right to serve tiles from its own site. to see the renderings,
Yes, it was MultiMap that Microsoft acquired together with a long term licence to serve OS 50K and 25K maps. The only free source I know of these is Bing Maps as the OS jealously guards these as the jewels in its crown. Anyone that want to sell them digitally has to implement draconian encryption and authorisation to avoid piracy of the maps. Yo used to be able to get them through a Mobac plugin but the OS stamped on that with a cease and desist letter. It can still be done but its a bit tortuous.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i suppose you're right, but to my eye, these captured a few seconds ago look just like what i remember from years ago.
 

Attachments

  • os1.png
    os1.png
    291.4 KB · Views: 149
  • os2.png
    os2.png
    213.9 KB · Views: 158
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
so, it's unclear whether you've figured this out or not. i'll submit one more example using webview before withdrawing.
 

Attachments

  • os1.png
    os1.png
    211.6 KB · Views: 133
Upvote 0

Ivica Golubovic

Active Member
Licensed User
No I haven't figured it out. You are posting screen shots but not giving any indication as to how you are obtaining them. Is your code available?
Is it necessary to use webview and what do you need in relation to Bing map? Do you only need a classic map display or do you also need some objects like markers, polygons and the like? Do you have a Bing API key?

I can recommend you another possibility to display the bing map without using webview. The quality of the map display is excellent, but you have to create a free API key for mobile devices.
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
I only want to display the maps. I have other offline mapping programs that I have written that will do GNSS, markers etc. I just want to display the online Bing Maps Ordnance Survey 50K and 25K maps, which I am assuming are up to date, to cross check with my existing offline 50K and 25K mapping most of which which dates from 2010, for any major road improvements or new housing developments. No I don't have a Bing API key.
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
Okay, now I understand your needs. I am currently working on OSMDroid version 6.1.14 which has support for displaying Bing maps, also HEREWEGo maps, MapQuest, MapBox, Thunderforest etc. If that would help you, I can send you the BETA version of the library.

WebView is not a standard browser like Firefox or Chrome and it is very difficult to get the results you want. Changing UserAgent to WebView causes more problems than good, so it's not really the best option.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I can send you the BETA version of the library.
Thanks for the kind offer, but there as there is no urgency for this and as you are already working on the new library I am happy to wait for the first release and play with it then.
 
Upvote 0
Top