amap_location 0.0.2

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

flutter_amap_location

android preview

ios preview

高德地图定位flutter组件。

目前实现直接获取定位和监听定位功能。

注意:随着flutter版本的提升, 本项目也会随之更新,

Getting Started

集成高德地图定位android版本

1、先申请一个apikey http://lbs.amap.com/api/android-sdk/guide/create-project/get-key

2、在AndroidManifest.xml中增加

 <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="你的Key" />

3、增加对应的权限:

    <!-- Normal Permissions 不需要运行时注册 -->
    <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

    <!-- 请求网络 -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- 不是SDK需要的权限,是示例中的后台唤醒定位需要的权限 -->
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <!-- 需要运行时注册的权限 -->
    <!-- 用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <!-- 用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <!-- 用于提高GPS定位速度 -->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    <!-- 写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <!-- 读取缓存数据 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <!-- 用于读取手机当前的状态 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <!-- 更改设置 -->
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

集成高德地图定位ios版本

1、申请一个key http://lbs.amap.com/api/ios-sdk/guide/create-project/get-key

直接在dart文件中设置key

import 'package:amap_location/amap_location.dart';
   
   void main(){     
       AMapLocationClient.setApiKey("你的key");
     runApp(new MyApp());
   }

2、在info.plist中增加:

<key>NSLocationWhenInUseUsageDescription</key>
<string>要用定位</string>

怎么用

先导入dart包 修改pubspec.yaml,增加依赖:

dependencies:
  amap_location: "^0.0.1"

在要用的地方导入:

import 'package:amap_location/amap_location.dart';

先启动一下

 await AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters  ));

直接获取定位:

await AMapLocationClient.getLocation(true)

监听定位


    AMapLocationClient.onLocationUpate.listen((AMapLocation loc){
      if(!mounted)return;
      setState(() {
         ...
      });
    });

    AMapLocationClient.startLocation();

停止监听定位

AMapLocationClient.stopLocation();

不要忘了在app生命周期结束的时候关闭

@override
  void dispose() {
    //注意这里关闭
    AMapLocationClient.shutdown();
    super.dispose();
  }

特性

  • IOS
  • Android
  • 直接获取定位
  • 监听定位改变

下个版本

  • 地理围栏监听

[0.0.1] - [2018-05-15]

  • ANDROID
  • IOS
  • 直接获取定位
  • 监听定位改变

[0.0.2] - [2018-05-15]

  • 更新README

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:amap_location/amap_location.dart';
import 'package:async_loader/async_loader.dart';

void main(){
  /*============*/
  //设置ios的key
  /*=============*/
  AMapLocationClient.setApiKey("a5bae506b2d053ed4ae7827f38b1766d");
  /*============*/
  //设置ios的key
  /*=============*/


  runApp(new MaterialApp(home: new Home(),routes:{
    "/location/get":(BuildContext context)=>new LocationGet(),
    "/location/listen":(BuildContext content)=>new LocationListen()
  },));
}

class _LocationGetState extends State{
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('直接获取定位'),
        ),
        body:new Center(child:new AsyncLoader(
          renderLoad:()=>new CircularProgressIndicator(),
          initState: () async=>await AMapLocationClient.getLocation(true),
          renderSuccess: ({data}){
            AMapLocation loc = data;
            return new Text(getLocationStr(loc));
          },
          renderError: ([error]){
            return new Text("定位失败");
          },
          
          
        )
        )
    );

  }

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

  @override
  void dispose() {

    //这里可以停止定位
    //AMapLocationClient.stopLocation();

    super.dispose();
  }
}


class LocationGet extends StatefulWidget{

  @override
  State<StatefulWidget> createState()=>new _LocationGetState();




}

String getLocationStr(AMapLocation loc){
  if(loc==null){
    return "正在定位";
  }

  if(loc.isSuccess()){
    if(loc.hasAddress()){
      return "定位成功: \n时间${loc.timestamp}\n经纬度:${loc.latitude} ${loc.longitude}\n 地址:${loc.formattedAddress} ";
    }else{
      return "定位成功: \n时间${loc.timestamp}\n经纬度:${loc.latitude} ${loc.longitude}\n ";
    }
  }else{
    return "定位失败: \n错误:{code=${loc.code},description=${loc.description}";
  }
}

class _LocationListenState extends State{
  String location;



  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('监听定位改变'),
        ),
        body:new Center(child: new Text(location),)
    );

  }

  @override
  void initState() {

    AMapLocationClient.onLocationUpate.listen((AMapLocation loc){
      if(!mounted)return;
      setState(() {
        location = getLocationStr(loc);
      });
    });

    location = getLocationStr(null);

    AMapLocationClient.startLocation();

    super.initState();
  }

  @override
  void dispose() {
    //注意这里停止监听
    AMapLocationClient.stopLocation();
    super.dispose();
  }
}

class LocationListen extends StatefulWidget{
  @override
  State<StatefulWidget> createState()=>new _LocationListenState();

}


class _HomeState extends State<Home>{

  @override
  void initState() {
    //启动客户端,这里设置ios端的精度小一点
    AMapLocationClient.startup(new AMapLocationOption( desiredAccuracy:CLLocationAccuracy.kCLLocationAccuracyHundredMeters  ));
    super.initState();
  }


  @override
  void dispose() {
    //注意这里关闭
    AMapLocationClient.shutdown();
    super.dispose();
  }


  List<Widget> render(BuildContext context,List children){
    return ListTile.divideTiles(context:context,tiles: children.map((dynamic data){
      return buildListTile(context, data["title"], data["subtitle"],data["url"]);
    })).toList();
  }


  Widget buildListTile(BuildContext context,String title,String subtitle,String url){
    return new ListTile(
      onTap: (){
        Navigator.of(context).pushNamed(url);
      },
      isThreeLine: true,
      dense: false,
      leading:null ,
      title: new Text(title),
      subtitle: new Text(subtitle),
      trailing: new Icon(Icons.arrow_right,color: Colors.blueAccent,),
    );
  }


  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: new AppBar(
          title: new Text('高德地图定位'),
        ),
        body: new Scrollbar(child: new ListView(
          children: render(context, [
            {"title":"直接获取定位","subtitle":"不需要先启用监听就可以直接获取定位","url":"/location/get"},
            {"title":"监听定位","subtitle":"启动定位改变监听","url":"/location/listen"}
          ]),

        ))
    );

  }

}

class Home extends StatefulWidget{


  @override
  State<StatefulWidget> createState() {
    return new _HomeState();
  }

}

Use this package as a library

1. Depend on it

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


dependencies:
  amap_location: "^0.0.2"

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:amap_location/amap_location.dart';
  
Version Uploaded Documentation Archive
0.0.2 May 16, 2018 Go to the documentation of amap_location 0.0.2 Download amap_location 0.0.2 archive
0.0.1 May 15, 2018 Go to the documentation of amap_location 0.0.1 Download amap_location 0.0.1 archive

Analysis

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

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3
  • Flutter: 0.5.4

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
51 / 100
Health:
Code health derived from static analysis. [more]
76 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
99 / 100
Overall score:
Weighted score of the above. [more]
68
Learn more about scoring.

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 9 warnings 2 hints.

    Strong-mode analysis of lib/amap_location.dart gave the following warning:

    line: 16 col: 14
    The final variable 'wifiAble' must be initialized.

    Strong-mode analysis of lib/amap_location_option.dart gave the following warning:

    line: 160 col: 3
    This function declares a return type of 'String', but doesn't end with a return statement.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • 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 API.

Dependencies

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