Commit 0df5a9a7 authored by 汪林玲's avatar 汪林玲

增加gps和打开gps判断

parent 2a783d1f
package com.lzq.tencent_map; package com.lzq.tencent_map;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.embedding.engine.plugins.FlutterPlugin;
...@@ -7,12 +13,14 @@ import io.flutter.plugin.common.MethodCall; ...@@ -7,12 +13,14 @@ import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/** /**
* TencentMapPlugin * TencentMapPlugin
*/ */
public class TencentMapPlugin implements FlutterPlugin, MethodCallHandler { public class TencentMapPlugin implements FlutterPlugin, MethodCallHandler {
public final static String TAG = "TencentMapPlugin";
/// The MethodChannel that will the communication between Flutter and native Android /// The MethodChannel that will the communication between Flutter and native Android
/// ///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it /// This local reference serves to register the plugin with the Flutter Engine and unregister it
...@@ -20,17 +28,36 @@ public class TencentMapPlugin implements FlutterPlugin, MethodCallHandler { ...@@ -20,17 +28,36 @@ public class TencentMapPlugin implements FlutterPlugin, MethodCallHandler {
private MethodChannel channel; private MethodChannel channel;
private Handler handler; private Handler handler;
private Context context;
@Override @Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
context = flutterPluginBinding.getApplicationContext();
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "tencent_map"); channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "tencent_map");
channel.setMethodCallHandler(this); channel.setMethodCallHandler(this);
handler = new Handler(flutterPluginBinding.getApplicationContext()); handler = new Handler(context);
} }
@Override @Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getLocation")) { if (call.method.equals("getLocation")) {
handler.getLocation(result); handler.getLocation(result);
}else if("isOpenGPS".equals(call.method)){
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gps = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
result.success(true);
}else{
result.success(false);
}
} else if("enableGPS".equals(call.method)){
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent);
result.success(true);
} else { } else {
result.notImplemented(); result.notImplemented();
} }
......
PODS: PODS:
- Flutter (1.0.0) - Flutter (1.0.0)
- tencent_map (0.0.1): - tencent_map (1.0.0):
- Flutter - Flutter
DEPENDENCIES: DEPENDENCIES:
...@@ -15,7 +15,7 @@ EXTERNAL SOURCES: ...@@ -15,7 +15,7 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS: SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
tencent_map: ac1cae67aa28e3b2bff2572e5ef5a282b74c1923 tencent_map: 2258908e7e400e6061a3bd0a173e2f2f9fa80fbf
PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
......
...@@ -23,7 +23,19 @@ class _MyAppState extends State<MyApp> { ...@@ -23,7 +23,19 @@ class _MyAppState extends State<MyApp> {
return MaterialApp( return MaterialApp(
home: Scaffold( home: Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Plugin example app'), title: const Text('Plugin'),
actions: [
IconButton(icon:Text('isGPS'), onPressed:(){
TencentMap.isOpenGPS().then((value){
print("----------------------------------GPS是否开启:$value");
});
}),
IconButton(icon:Text('openGPS'), onPressed:(){
TencentMap.enableGPS().then((value){
print("----------------------------------打开GPS:$value");
});
})
],
), ),
body: Center( body: Center(
child: GestureDetector( child: GestureDetector(
......
...@@ -127,7 +127,7 @@ packages: ...@@ -127,7 +127,7 @@ packages:
path: ".." path: ".."
relative: true relative: true
source: path source: path
version: "0.0.1" version: "1.0.0"
term_glyph: term_glyph:
dependency: transitive dependency: transitive
description: description:
......
#import "TencentMapPlugin.h" #import "TencentMapPlugin.h"
#import <TencentLBS/TencentLBS.h> #import <TencentLBS/TencentLBS.h>
#import <CoreLocation/CoreLocation.h>
@interface TencentMapPlugin()<TencentLBSLocationManagerDelegate> @interface TencentMapPlugin()<TencentLBSLocationManagerDelegate>
...@@ -48,6 +49,17 @@ ...@@ -48,6 +49,17 @@
result(data); result(data);
} }
}]; }];
}else if ([@"isOpenGPS" isEqualToString:call.method]) {
if ([CLLocationManager locationServicesEnabled] == YES){
result(@YES);
}else{
result(@NO);
}
} else if ([@"enableGPS" isEqualToString:call.method]) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
} else { } else {
result(FlutterMethodNotImplemented); result(FlutterMethodNotImplemented);
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# #
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'tencent_map' s.name = 'tencent_map'
s.version = '0.0.1' s.version = '1.0.0'
s.summary = 'A new flutter plugin project.' s.summary = 'A new flutter plugin project.'
s.description = <<-DESC s.description = <<-DESC
A new flutter plugin project. A new flutter plugin project.
......
...@@ -5,10 +5,29 @@ import 'package:flutter/services.dart'; ...@@ -5,10 +5,29 @@ import 'package:flutter/services.dart';
class TencentMap { class TencentMap {
static const MethodChannel _channel = const MethodChannel('tencent_map'); static const MethodChannel _channel = const MethodChannel('tencent_map');
///
/// 获取GPS定位
/// [apiKey] 腾讯地图官方库
///
static Future<Location> getLocation({String apiKey}) async { static Future<Location> getLocation({String apiKey}) async {
Map map = await _channel.invokeMethod('getLocation',{'apiKey':apiKey}); Map map = await _channel.invokeMethod('getLocation',{'apiKey':apiKey});
return Location.form(map); return Location.form(map);
} }
///
/// 是否打开GPS
///
static Future<bool> isOpenGPS() async {
return await _channel.invokeMethod('isOpenGPS');
}
///
/// 启用GPS
///
static Future<bool> enableGPS() async {
return await _channel.invokeMethod('enableGPS');
}
} }
class Location { class Location {
......
name: tencent_map name: tencent_map
description: A new Flutter plugin. description: A new Flutter plugin.
version: 0.0.1 version: 1.0.0
author: author:
homepage: homepage:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment