gif如下所示(文件有点大,已压缩)
本文章为Qt Quick/QML入门到精通付费专栏部分文章,Demo已经上传至免费交流群779866667
缓存目录如下图所示,当有缓存时,则会获取该缓存目录,否则在线联网更新数据:
qml提供了四个内建的地图在线插件"esri"、"mapbox"、"nokia"、"osm",但是都不是国内地图,对国内很不友好,本章我们实现一个高德地图插件,我们以osm插件为例,如下所示:
{
"Keys": ["osm"],
"Provider": "osm",
"Version": 100,
"Experimental": false,
"Features": [
"OnlineMappingFeature",
"OnlineGeocodingFeature",
"ReverseGeocodingFeature",
"OnlineRoutingFeature",
"OnlinePlacesFeature"
]
}
其中"Features"表示该插件支持的哪些特性,比如"OnlineMappingFeature"表示支持在线地图显示、则我们需要实现一个QGeoMappingManagerEngine引擎
每个引擎能支持的所有功能介绍如下所示:
QGeoRoutingManagerEngine
NoRoutingFeatures - Routing is not supported (KO: this option can be omitted)
OnlineRoutingFeature - Online routing support
OfflineRoutingFeature - Offline routing support
LocalizedRoutingFeature - Localized routing support
RouteUpdatesFeature - Support for dynamic path update
AlternativeRoutesFeature - Support for multiple alternative routes
ExcludeAreasRoutingFeature - Support for excluding routing factors
AnyRoutingFeatures - Supports anything your heart desires for routing
QGeoCodingManagerEngine
NoGeocodingFeatures - Geocoding is not supported
OnlineGeocodingFeature - Online geocoding support
OfflineGeocodingFeature - Offline geocoding support
ReverseGeocodingFeature - Reverse geocoding support
LocalizedGeocodingFeature - Localized geocoding support
AnyGeocodingFeatures - All of the above except NoGeocodingFeatures
QGeoMappingManagerEngine
NoMappingFeatures - No mapping supported
OnlineMappingFeature - Support for online maps
OfflineMappingFeature - Support for offline maps
LocalizedMappingFeature - Support for maps with localization
AnyMappingFeatures - All of the above except NoMappingFeatures
QPlaceManagerEngine
NoPlacesFeatures - Point-of-interest is not supported
OnlinePlacesFeature - Online Point-of-interest support
OfflinePlacesFeature - Offline Point-of-interest support
SavePlaceFeature - Support for saving custom points to the map
RemovePlaceFeature - Support for removing Point-of-interes on a map
SaveCategoryFeature - Create and save custom Point-of-interest categories
RemoveCategoryFeature - Remove Point-of-interest categories
PlaceRecommendationsFeature - Support for Recommended Point-of-interest according to keywords
SearchSuggestionsFeature - Suggestions support according to the search query part
LocalizedPlacesFeature - Localization support for Point-of-interes
NotificationsFeature - Support for Point-of-interes change notifications
PlaceMatchingFeature - Support for Point-of-interes comparison from two different providers
AnyPlacesFeatures - It's Time for You to Relax
实现一个QT地图服务插件,我们需要子类化QGeoServiceProviderFactory,以及实现 4个接口虚函数:
QGeoCodingManagerEngine* createGeocodingManagerEngine() : 位置搜索引擎,实现位置地理编码,比如配置文件有"OnlineGeocodingFeature"相关特性,则需要实现该虚函数
QGeoMappingManagerEngine* createMappingManagerEngine() : 地图映射管理器引擎,实现地图显示,比如配置文件有"OnlineMappingFeature"相关特性,则需要实现该虚函数
QGeoRoutingManagerEngine* createRoutingManagerEngine() : 路径规划引擎,实现路径规划,比如配置文件有"OnlineRoutingFeature"相关特性,则需要实现该虚函数
QPlaceManagerEngine* createPlaceManagerEngine() : 位置搜索引擎,实现位置搜索,比如配置文件有"OnlinePlacesFeature"相关特性,则需要实现该虚函数
由于本章我们只需要实现一个在线高德地图显示,所以只需要实现QGeoMappingManagerEngine* createMappingManagerEngine()虚函数即可.
自定义一个GeoServiceProviderFactory类,并且继承于QGeoServiceProviderFactory类.
如果不生成dll的话,则需要在main函数中声明插件类:
Q_IMPORT_PLUGIN(GeoServiceProviderFactory)
GeoServiceProviderFactory代码如下所示:
class GeoServiceProviderFactory: public QObject, public QGeoServiceProviderFactory
{
Q_OBJECT
Q_INTERFACES(QGeoServiceProviderFactory)
Q_PLUGIN_METADATA(IID "org.qt-project.qt.geoservice.serviceproviderfactory/5.0" FILE "maps_plugin.json")
public:
QGeoMappingManagerEngine* createMappingManagerEngine (const QVariantMap ¶meters,
QGeoServiceProvider::Error *error, QString *errorString) const; // 返回我们子类化的地图映射管理器引擎类
};
Q_PLUGIN_METADATA宏用来描述该属性的元信息
IID "org.qt-project.qt.geoservice.serviceproviderfactory/5.0" 则用来描述地图服务的通用接口描述符、
FILE "map_plugin.json" 则标明我们插件的配置描述文件是谁.
map_plugin.json配置文件如下所示:
{
"Keys": ["Gaode"],
"Provider": "Gaode",
"Version": 100,
"Experimental": false,
"Features": [
"OnlineMappingFeature",
"OnlineGeocodingFeature",
"ReverseGeocodingFeature"
]
}
假如我们qml去使用Gaode插件,如下所示:
Map {
id: _map
anchors.fill: parent
// 地图插件
plugin: Plugin { name: "Gaode" }
}
那么就会根据"Gaode"去找到GeoServiceProviderFactory的createMappingManagerEngine虚函数,从而注册一个地图映射管理器引擎给qml显示地图用.
61.qt quick-qml高德地图实现V1版本(跨平台支持、无需浏览器内核,运行迅速,下个版本实现位置搜索)_心中无女人 拔刀自然神 剑谱第一页 忘掉心上人-CSDN博客
63.qt quick-qml使用高德地图插件实现V2版本(新增:位置搜索、路径规划、轨迹编辑等)_获得黑色生命力,抵抗时间扭曲,坚持去做一件有长期有意义的事-CSDN博客
订阅专栏可以走的更远,所有文章都有demo(包括合成大西瓜源码、微信聊天源码、listview拖拽源码等)