一、地图插件介绍
 1.qml提供了四个内建的地图在线插件"esri"、“mapbox”、“nokia”、“osm”,但是都不是国内地图,对国内很不友好,esri国内可以使用。要是想使用高德和谷歌需要自己写插件。
例如:
 插件文件gd_plugin.json
{
    "Keys": ["amap"],
    "Provider": "amap",
    "Version": 200,
    "Experimental": false,
    "Features": [
        "OnlineGeocodingFeature",
        "ReverseGeocodingFeature",
        "OnlineRoutingFeature",
        "AlternativeRoutesFeature",
        "OnlineMappingFeature",
        "SearchSuggestionsFeature"
    ]
}2.其中"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
3.QGeoServiceProviderFactory地图服务插件实现
 实现一个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 "gd_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 “gd_plugin.json” 则标明我们插件的配置描述文件是谁.
假如我们qml去使用amap插件,如下所示:
Map {
id: _map
anchors.fill: parent
// 地图插件
plugin: Plugin { name: "amap" }
}
那么就会根据"amap"去找到GeoServiceProviderFactory的createMappingManagerEngine虚函数,从而注册一个地图映射管理器引擎给qml显示地图用.









