问题
tldr: recently, my use of the Google Maps JavaScript API v3 does not show routes or markers anymore. It works fine on my Debian-box, but not on any Windows computer I tried (ranging XP to Windows 8). Has there been a recent change in the API? Is my code buggy? see http://pastebin.com/ubRGPtDZ
Dear All,
in August 2014, I wrote a Qt program that at some stage shows Google Maps in a QWebView . Through JavaScript I was able to draw a route.
This program has been functioning fine, up to mid-December. On my Linux-box (Debian), it still works fine, but on every Windows computer I try, stops showing the route. Google Maps itself shows fine, but it stops showing any route, any marker, ...
Did anything change in Google Maps mid-December 2014? I checked the changelog of Javascript Maps API v3 , but I do not see any new mention after September 2014.
Should I check somewhere else?
Or is there any change in QWebView-behaviour on Windows-boxes since mid December? Is that even possible?
I still hold possible that my JavaScript tweaking is inadequate, so I will debug that code as well, but definitely baffles me that everything worked fine for over 4 months on Windows-computers, and still does on my Debian-Box. I have put it online at http://pastebin.com/ubRGPtDZ .
My apologies for any bad English, non-native speaker here.
Thank you all for any suggestion.
Wim
回答1:
Found it! This was a very frustrating challenge, but it feels great now that it is solved. It took me days of browsing through any documentation possible, of pure trial-and-error, testing and doubting every aspect of my code or even configuration, all failed... But one more time I browsed the qt-project and found this post by lowsnr and Yuvalal.
In the words of lowsnr:
as part of the test that the Google code performs to determine whether the browser is touch enabled, it checks the User Agent for Chrome >= 5.0, so by changing the user agent to something like ‘Chrome/1.0’ the touch interface suppressed.
Apparently it is also needed for displaying the directions (*). So I wrapped up a new class called myWebPage and derived it from qWebPage:
class myWebPage : public QWebPage
{
virtual QString userAgentForUrl(const QUrl& url) const {
return "Chrome/1.0";
}
};
and before loading the HTML in the qWebView, I use setPage():
ui->setupUi(this);
ui->webView->setPage(new myWebPage());
ui->webView->setHtml("...");
Now it works like a charm :-)
--
(*) For me, the prove that the problem was not my JavaScript, is that I tried to simply load the same HTML script as used in the Google Maps code examples. In a QWebView, it works fine for my Linux-box, but refuses to show the directions on any Windows-compiled binary that I tried. Still, the code works fine if you load to a regular browser.
Conclusion: Google Maps does not necessary like QWebView.
来源:https://stackoverflow.com/questions/27730642/google-maps-draw-route-malfunction-in-windows