Blame view
app/src/main/java/com/dinhcv/lifelogpedometer/activity/MapFragment.java
4.94 KB
7f095a929
|
1 |
package com.dinhcv.lifelogpedometer.activity; |
6b038e443
|
2 |
import android.Manifest; |
7f095a929
|
3 4 |
import android.app.Activity; import android.content.Context; |
6b038e443
|
5 6 7 8 |
import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; |
7f095a929
|
9 10 |
import android.os.Bundle; import android.support.annotation.Nullable; |
6b038e443
|
11 12 |
import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; |
7f095a929
|
13 14 15 16 17 18 19 |
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; |
6b038e443
|
20 |
import com.google.android.gms.maps.CameraUpdateFactory; |
7f095a929
|
21 22 |
import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; |
6b038e443
|
23 24 25 26 27 28 |
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; |
7f095a929
|
29 30 31 |
public class MapFragment extends Fragment implements SettingFragmentPresenter, OnMapReadyCallback { private View mRootView; |
6b038e443
|
32 33 34 35 36 37 38 39 40 41 42 43 44 |
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; |
7f095a929
|
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
@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) { |
6b038e443
|
65 66 67 68 |
mapFragment = (SupportMapFragment) getChildFragmentManager() .findFragmentById(R.id.map); polylineOptions.color(ContextCompat.getColor(getActivity(),android.R.color.holo_red_dark)); |
7f095a929
|
69 70 71 72 73 74 75 |
} /** * Init data */ private void initData() { |
6b038e443
|
76 |
mLocationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); |
7f095a929
|
77 |
|
6b038e443
|
78 79 80 81 82 |
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); |
7f095a929
|
83 |
} |
6b038e443
|
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 |
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); } } |
7f095a929
|
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 |
@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 |
6b038e443
|
159 160 161 162 163 164 165 |
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)); |
7f095a929
|
166 |
Log.i("SonLT","Readdy"); |
6b038e443
|
167 168 |
//updateLine(); |
7f095a929
|
169 170 |
} } |