MapFragment.java
4.94 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.dinhcv.lifelogpedometer.activity;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.dinhcv.lifelogpedometer.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import static android.content.Context.LOCATION_SERVICE;
public class MapFragment extends Fragment implements SettingFragmentPresenter, OnMapReadyCallback {
private View mRootView;
private PolylineOptions polylineOptions = new PolylineOptions();
private LocationManager mLocationManager;
// The minimum distance to change Updates in meters
private static final long LOCATION_REFRESH_DISTANCE = 10;
// The minimum time between updates in milliseconds
private static final long LOCATION_REFRESH_TIME = 10000;
private double lat = 0;
private double log = 0;
private SupportMapFragment mapFragment;
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_map, container, false);
initView(mRootView);
initData();
return mRootView;
}
/**
* init view
*
* @param rootView view
*/
private void initView(View rootView) {
mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
polylineOptions.color(ContextCompat.getColor(getActivity(),android.R.color.holo_red_dark));
}
/**
* Init data
*/
private void initData() {
mLocationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);
}
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//your code here
lat = location.getLatitude();
log = location.getLongitude();
polylineOptions.add(new LatLng(lat, log));
if (googleMap!= null) {
googleMap.addPolyline(polylineOptions);
}
//update
update();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
private void update(){
mapFragment.getMapAsync(this);
}
private void updateLine(){
LatLng sydney = new LatLng(-34, 151);
LatLng melbourne=new LatLng(37.813, 144.9631);
LatLng auckland=new LatLng(36.8485, 174.7633);
polylineOptions.add(new LatLng[]{sydney,melbourne,auckland});
if (googleMap!= null) {
googleMap.addPolyline(polylineOptions);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
}
}
/**
* Save data
*/
@Override
public void onSaveData() {
}
@Override
public void onInvalidate(boolean isInit) {
initData();
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
initData();
}
@Override
public void onMapReady(GoogleMap gm) {
googleMap = gm;
LatLng pos=new LatLng(lat, log);
googleMap.addMarker(new MarkerOptions()
.position(pos));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 15.0f));
Log.i("SonLT","Readdy");
//updateLine();
}
}