flutter_amap_location 0.0.5

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new50

flutter_amap_location

A Flutter plugin for AMap location. 高德地图定位插件

在0.0.1版本中,使用了场景定位来获取定位数据。 在0.0.2版本中,修改了接口方法,尽量按高德定位sdk的接口来,这样高德定位SDK的文档也可以提供一定的参考意义。

使用方法

引入flutter_amap_location包

pubspec.yaml文件中dependencies:下加入

flutter_amap_location:

然后在终端下执行

flutter packages get 

完成后,再引入包

import 'package:flutter_amap_location/flutter_amap_location.dart';

引入包以后,只需要在您的flutter程序中增加两个函数,用来处理接收到的数据和发生的错误。

有个文章可以参考一下。

  • 连续定位 高德定位SDK默认使用的是连续定位方式,您只需设置相关定位参数,然后调用startLocation()方法即可; 如果不需要调整定位参数,直接使用默认参数,则更简单,直接调用startLocation(),开始定位,定位成功后结果通过 StreamHandler的事件发送回Flutter端。
// 设置定位参数
// ...
// 开始定位
startLocation();

注意:连续定位默认时间间隔是2000ms,可通过setInterval()改变。当不需要定位时,调用stopLocation()停止定位服务。

  • 单次定位 单次定位有两种方法实现,一个是调用setOnceLocation()设置定位选项,一个是通过setOnceLocationLatest()设置定位 选项后,再调用startLocation()启动定位,定位结果同样通过StreamHandler的事件发送回Flutter端。
// 只定位一次
setOnceLocation(true);
startLocation();

// 只定位一次,返回最近3秒内定位结果中精度最高的一次结果
setOnceLocationLatest(true);
startLocation();

注意:使用setOnceLocationLatest()时,会同时将setOnceLocation()的参数设置为相同的值;反之则不会。

  • 定位场景 从高德定位sdk3.7.0开始,sdk已经提供了按场景定位的功能,提供了三种预置的场景:签到、出行、运动,默认为无场景。 如果选择用场景定位,则无需设置其它定位参数,sdk会根据选择的场景自动设置参数。 当然开发人员仍可以根据自己的需要设置定位参数。在设置完场景后,最好执行一次stopLocation(),再startLocation(), 以确保场景参数生效。
setLocationPurpose(AMapLocationPurpose.TRANSPORT);
stopLocation();
startLocation();
  • 设置定位参数的方法
setOnceLocation()       设置只定位一次
setOnceLocationLatest() 设置只定位一次,返回3秒内的定位结果中精度最高的的结果
setInterval()           设置定位间隔,默认为2000ms
setHttpTimeOut()        设置定位超时时间,默认30s
setLocationMode()       设置定位模式,默认为高精度
setNeedAddress()        设置是否返回地址信息,默认是返回
setMockEnable()         设置是否支持模拟定位,默认是支持
setLocationCacheEnable() 设置是否使用缓存,默认是使用
setLocationPurpose()    设置定位场景

注意事项

  • andriod下,要在AndroidManifest.xml增加定位权限
    <!--用于进行网络定位-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <!--用于访问GPS定位-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <!--用于获取运营商信息,用于支持提供运营商信息相关的接口-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <!--用于访问wifi网络信息,wifi信息会用于进行网络定位-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
    <!--用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
    <!--用于访问网络,网络定位需要上网-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <!--用于读取手机当前的状态-->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
    <!--用于写入缓存数据到扩展存储卡-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <!--用于申请调用A-GPS模块-->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <!--用于申请获取蓝牙信息进行室内定位-->
    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
  • 增加一个定位service和设置高德定位的apikey
   <service android:name="com.amap.api.location.APSService"></service>

   <meta-data android:name="com.amap.api.v2.apikey" android:value="你在高德后台获取的apikey">
   </meta-data>

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

0.0.5

  • 测试pub.flutter-io.cn是否存在故障

0.0.4

  • 修改了README.md
  • 同时测试pub.flutter-io.cn是否存在故障

0.0.3

  • 去掉了获取platform版本号的方法
  • 增加api 文档

0.0.2

  • 改写插件,完成对高德定位sdk的主要接口的封装,使插件接口基本与sdk保持一致

0.0.1

  • 使用高德定位sdk的签到和出行场景,实现定位功能
  • TODO:完成ios的支持

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_amap_location/flutter_amap_location.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _address = "未知位置";
  double _longitude = 0.0;
  double _latitude  = 0.0;

  @override
  void initState() {
    super.initState();

    FlutterAmapLocation.listenLocation(_onLocationEvent, _onLocationError);
  }

  Future<void> getLocationOnce() async {
    String address;
    try {
      FlutterAmapLocation.setOnceLocation(true);
      await FlutterAmapLocation.startLocation();
    } on PlatformException catch (e) {
      address = "Failed to get address: '${e.message}'";
    }

    setState(() {
      _address = address;
    });
  }

  Future<void> getLocation() async {
    String address;
    try {
      FlutterAmapLocation.setOnceLocation(false);
      FlutterAmapLocation.setOnceLocationLatest(false);
      await FlutterAmapLocation.startLocation();
    } on PlatformException catch (e) {
      address = "Failed to get address: '${e.message}'";
    }

    setState(() {
      _address = address;
    });
  }

  Future<void> stopLocation() async {
    try {
      await FlutterAmapLocation.stopLocation();
    } on PlatformException catch (e) {
       print( "Failed to stop location: '${e.message}'");
    }
  }

  void _onLocationEvent(Object event) {
    Map<String, Object> loc = Map.castFrom(event);
    print (loc['address']);

    setState(() {
      _longitude = loc['longitude'];
      _latitude = loc['latitude'];
      _address = loc['address'];
    });
  }

  void _onLocationError(Object event) {
    print(event);
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),

        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  new RaisedButton(onPressed: getLocationOnce,
                    child: new Text("定位一次"),),
                  new RaisedButton(onPressed: getLocation,
                    child: new Text("连续定位"),),
                  new RaisedButton(onPressed: stopLocation,
                    child: new Text("停止定位"),),
                ],
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                new Text("经度:$_longitude"),
                ],
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text("纬度:$_latitude"),
                ],
              ),
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Expanded(child: new Text("地址:$_address"),),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_amap_location: ^0.0.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_amap_location/flutter_amap_location.dart';
  
Version Uploaded Documentation Archive
0.0.5 Aug 5, 2018 Go to the documentation of flutter_amap_location 0.0.5 Download flutter_amap_location 0.0.5 archive
0.0.4 Aug 5, 2018 Go to the documentation of flutter_amap_location 0.0.4 Download flutter_amap_location 0.0.4 archive
0.0.3 Aug 4, 2018 Go to the documentation of flutter_amap_location 0.0.3 Download flutter_amap_location 0.0.3 archive
0.0.2 Aug 4, 2018 Go to the documentation of flutter_amap_location 0.0.2 Download flutter_amap_location 0.0.2 archive
0.0.1 Jul 26, 2018 Go to the documentation of flutter_amap_location 0.0.1 Download flutter_amap_location 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Aug 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.11.8
  • Flutter: 0.5.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.58.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.11
meta 1.1.5 1.1.6
sky_engine 0.0.99
typed_data 1.1.5 1.1.6
vector_math 2.0.6 2.0.8