admin
2022-01-12 8327000a0cce5e47226372e0e25c1e6faec497e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.yeshi.location.plugins;
 
import android.content.Context;
import android.location.Address;
import android.util.Log;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.yeshi.location.utils.LocationUtils;
 
import java.util.HashMap;
import java.util.Map;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.StandardMessageCodec;
 
public class LocationPlugins implements BasicMessageChannel.MessageHandler<Object> {
 
    private static final String TAG = "LocationUtil";
 
    private final Context activity;
    private final BasicMessageChannel<Object> messageChannel;
 
 
    private LocationPlugins(Context activity, BinaryMessenger messager) {
        this.activity = activity;
        this.messageChannel = new BasicMessageChannel<Object>(messager, "LocationUtil", StandardMessageCodec.INSTANCE);
        messageChannel.setMessageHandler(this);
    }
 
    public static LocationPlugins registerWith(Context activity, BinaryMessenger messager) {
        return new LocationPlugins(activity, messager);
    }
 
 
    @Override
    public void onMessage(@Nullable Object message, @NonNull BasicMessageChannel.Reply<Object> reply) {
        LocationUtils.getInstance(activity).setAddressCallback(new LocationUtils.AddressCallback() {
            @Override
            public void onGetAddress(Address address) {
                Map<String, String> map = new HashMap<>();
                map.put("lat", address.getLatitude() + "");
                map.put("lng", address.getLongitude() + "");
                String addressLine = "";
                for (int i = 0; address.getAddressLine(i) != null; i++) {
                    addressLine = address.getAddressLine(i);
                }
                map.put("address", addressLine);
                map.put("addressDetail", address.getFeatureName());
                Log.i(TAG, new Gson().toJson(address));
                try {
                    reply.reply(map);
                } catch (Exception e) {
                }
            }
 
            @Override
            public void onGetLocation(double lat, double lng) {
                Log.i(TAG, String.format("定位经纬度 lat:%s lng:%s", lat, lng));
                Map<String, String> map = new HashMap<>();
                map.put("lat", lat + "");
                map.put("lng", lng + "");
                reply.reply(map);
            }
        });
    }
}