-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapActivity.java
242 lines (196 loc) · 8.29 KB
/
MapActivity.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.li.tritonia.wildlife;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.List;
public class MapActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {
final int RQS_GooglePlayServices = 1;
private String TAG = "GPSTEST";
private TextView mLocationView;
private GoogleApiClient mGoogleApiClient;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
double lat, lon;
int taskIdVal, huntIdVal;
double taskLat, taskLon;
GoogleMap googleMap;
Button foundTaskBtn, moveMapBtn;
LocationRequest mLocationRequest;
Location mCurrentLocation;
Marker userMarker, taskMarker;
Intent intent;
Polyline polyline;
PolylineOptions polylineOptions;
MarkerOptions userMarkerOptions, taskMarkerOptions;
List<LatLng> points;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationView = new TextView(this);
setContentView(R.layout.activity_map);
// Location awareness API
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
MapFragment map = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
map.getMapAsync(this);
intent = getIntent();
taskIdVal = intent.getIntExtra("taskIdValue", 0);
huntIdVal = intent.getIntExtra("huntIdValue", 0);
taskLat = MainActivity.dataBase.getGPSLat(taskIdVal, huntIdVal);
taskLon = MainActivity.dataBase.getGPSLon(taskIdVal, huntIdVal);
foundTaskBtn = (Button)findViewById(R.id.foundTaskButton);
moveMapBtn = (Button)findViewById(R.id.moveMapToUserButton);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
@Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
Toast.makeText(getApplicationContext(), "Google Play services is available.",
Toast.LENGTH_LONG).show();
} else {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices).show();
}
foundTaskBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent moveToTapActivity = new Intent(MapActivity.this, CheckpointTapActivity.class);
moveToTapActivity.putExtra("taskIdValue", taskIdVal);
moveToTapActivity.putExtra("huntIdValue", huntIdVal);
startActivity(moveToTapActivity);
}
});
moveMapBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
moveToMe();
}
});
userMarkerOptions = new MarkerOptions().position(new LatLng(50.674129, -120.334982)).title("It's me");
taskMarkerOptions = new MarkerOptions().position(new LatLng(50.674129, -120.334982)).title("Find Me");
polylineOptions = new PolylineOptions().add(new LatLng(50.674129, -120.334982), new LatLng(taskLat, taskLon)).width(5).color(Color.RED);
polyline = googleMap.addPolyline(polylineOptions);
userMarker = googleMap.addMarker(userMarkerOptions);
taskMarker = googleMap.addMarker(taskMarkerOptions);
}
@Override
protected void onStop() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
return super.onOptionsItemSelected(item);
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}
protected void moveToMe(){
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLocation.getLatitude()
, mCurrentLocation.getLongitude()), 24));
}
@Override
public void onConnected(Bundle bundle) {
createLocationRequest();
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "GoogleApiClient connection has been suspend");
}
@Override
public void onLocationChanged(Location location) {
lat = location.getLatitude();
lon = location.getLongitude();
List<LatLng> points = polyline.getPoints();
points.remove(1);
points.add(new LatLng(lat, lon));
polyline.setPoints(points);
userMarker.setPosition(new LatLng(lat, lon));
mCurrentLocation = location;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) { // Log the error
e.printStackTrace();
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, CONNECTION_FAILURE_RESOLUTION_REQUEST).show();
}
}
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
// Decide what to do based on the original request code
switch (requestCode) {
case CONNECTION_FAILURE_RESOLUTION_REQUEST :
/*
* If the result code is Activity.RESULT_OK, try * to connect again
*/
switch (resultCode) {
case Activity.RESULT_OK : /*
* Try the request again
*/
break;
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
}