001.
package
com.example.crmtracking.fromweb;
002.
003.
import
android.content.Context;
004.
import
android.graphics.Color;
005.
import
android.os.Bundle;
006.
import
android.support.v4.app.FragmentActivity;
007.
import
android.view.View;
008.
import
android.widget.Button;
009.
import
android.widget.Toast;
010.
011.
import
com.google.android.gms.common.ConnectionResult;
012.
import
com.google.android.gms.common.GooglePlayServicesUtil;
013.
import
com.google.android.gms.location.Geofence;
014.
import
com.google.android.gms.maps.CameraUpdateFactory;
015.
import
com.google.android.gms.maps.GoogleMap;
016.
import
com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
017.
import
com.google.android.gms.maps.SupportMapFragment;
018.
import
com.google.android.gms.maps.model.CameraPosition;
019.
import
com.google.android.gms.maps.model.CircleOptions;
020.
import
com.google.android.gms.maps.model.LatLng;
021.
import
com.google.android.gms.maps.model.MarkerOptions;
022.
023.
import
java.util.ArrayList;
024.
025.
026.
public
class
MainActivity
extends
FragmentActivity
implements
OnCameraChangeListener {
027.
028.
private
GoogleMap mMap;
029.
030.
031.
ArrayList<Geofence> mGeofences;
032.
033.
034.
ArrayList<LatLng> mGeofenceCoordinates;
035.
036.
037.
ArrayList<Integer> mGeofenceRadius;
038.
039.
040.
private
GeofenceStore mGeofenceStore;
041.
042.
043.
044.
045.
@Override
046.
protected
void
onCreate(Bundle savedInstanceState) {
047.
super
.onCreate(savedInstanceState);
048.
setContentView(R.layout.activity_main);
049.
050.
GoogleMap mMap = ((SupportMapFragment)getSupportFragmentManager()
051.
.findFragmentById(R.id.map)).getMap();
052.
053.
mMap.addMarker(
new
MarkerOptions().position(
054.
new
LatLng(
13.729468
,
100.779361
))
055.
.title(
"คณะวิทยาศาสตร์"
));
056.
mMap.addMarker(
new
MarkerOptions().position(
057.
new
LatLng(
13.726566
,
100.780104
))
058.
.title(
"คณะเทคโนโลยีการเกษตร"
));
059.
mMap.addMarker(
new
MarkerOptions().position(
060.
new
LatLng(
13.725534
,
100.776338
))
061.
.title(
"คณะสถาปัตยกรรมศาสตร์"
));
062.
mMap.addMarker(
new
MarkerOptions().position(
063.
new
LatLng(
13.727108
,
100.773323
))
064.
.title(
"คณะวิศวะกรรมศาสตร์"
));
065.
066.
067.
068.
069.
mGeofences =
new
ArrayList<Geofence>();
070.
mGeofenceCoordinates =
new
ArrayList<LatLng>();
071.
mGeofenceRadius =
new
ArrayList<Integer>();
072.
073.
074.
075.
mGeofenceCoordinates.add(
new
LatLng(
13.729468
,
100.779361
));
076.
mGeofenceCoordinates.add(
new
LatLng(
13.726566
,
100.780104
));
077.
mGeofenceCoordinates.add(
new
LatLng(
13.725534
,
100.776338
));
078.
mGeofenceCoordinates.add(
new
LatLng(
13.727108
,
100.773323
));
079.
080.
081.
082.
mGeofenceRadius.add(
120
);
083.
mGeofenceRadius.add(
120
);
084.
mGeofenceRadius.add(
120
);
085.
mGeofenceRadius.add(
120
);
086.
087.
088.
089.
090.
mGeofences.add(
new
Geofence.Builder()
091.
.setRequestId(
"คณะวิทยาศาสตร์"
)
092.
093.
.setCircularRegion(mGeofenceCoordinates.get(
0
).latitude, mGeofenceCoordinates.get(
0
).longitude, mGeofenceRadius.get(
0
).intValue())
094.
.setExpirationDuration(Geofence.NEVER_EXPIRE)
095.
096.
.setLoiteringDelay(
30000
)
097.
.setTransitionTypes(
098.
Geofence.GEOFENCE_TRANSITION_ENTER
099.
| Geofence.GEOFENCE_TRANSITION_DWELL
100.
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
101.
102.
103.
104.
mGeofences.add(
new
Geofence.Builder()
105.
.setRequestId(
"คณะเทคโนโลยีการเกษตร"
)
106.
107.
.setCircularRegion(mGeofenceCoordinates.get(
1
).latitude, mGeofenceCoordinates.get(
1
).longitude, mGeofenceRadius.get(
1
).intValue())
108.
.setExpirationDuration(Geofence.NEVER_EXPIRE)
109.
110.
.setLoiteringDelay(
30000
)
111.
.setTransitionTypes(
112.
Geofence.GEOFENCE_TRANSITION_ENTER
113.
| Geofence.GEOFENCE_TRANSITION_DWELL
114.
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
115.
116.
117.
mGeofences.add(
new
Geofence.Builder()
118.
.setRequestId(
"คณะสถาปัตยกรรมศาสตร์"
)
119.
120.
.setCircularRegion(mGeofenceCoordinates.get(
2
).latitude, mGeofenceCoordinates.get(
2
).longitude, mGeofenceRadius.get(
2
).intValue())
121.
.setExpirationDuration(Geofence.NEVER_EXPIRE)
122.
.setLoiteringDelay(
30000
)
123.
.setTransitionTypes(
124.
Geofence.GEOFENCE_TRANSITION_ENTER
125.
| Geofence.GEOFENCE_TRANSITION_DWELL
126.
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
127.
128.
129.
mGeofences.add(
new
Geofence.Builder()
130.
.setRequestId(
"คณะวิศวะกรรมศาสตร์"
)
131.
132.
.setCircularRegion(mGeofenceCoordinates.get(
3
).latitude, mGeofenceCoordinates.get(
3
).longitude, mGeofenceRadius.get(
3
).intValue())
133.
.setExpirationDuration(Geofence.NEVER_EXPIRE)
134.
.setLoiteringDelay(
30000
)
135.
.setTransitionTypes(
136.
Geofence.GEOFENCE_TRANSITION_ENTER
137.
| Geofence.GEOFENCE_TRANSITION_DWELL
138.
| Geofence.GEOFENCE_TRANSITION_EXIT).build());
139.
140.
141.
142.
143.
144.
mGeofenceStore =
new
GeofenceStore(
this
, mGeofences);
145.
146.
btnShow = (Button) findViewById(R.id.Bsearch);
147.
}
148.
149.
@Override
150.
protected
void
onStart() {
151.
152.
super
.onStart();
153.
}
154.
155.
@Override
156.
protected
void
onStop() {
157.
mGeofenceStore.disconnect();
158.
super
.onStop();
159.
160.
161.
}
162.
163.
@Override
164.
protected
void
onResume() {
165.
super
.onResume();
166.
if
(GooglePlayServicesUtil.isGooglePlayServicesAvailable(
this
) == ConnectionResult.SUCCESS) {
167.
setUpMapIfNeeded();
168.
}
else
{
169.
GooglePlayServicesUtil.getErrorDialog(
170.
GooglePlayServicesUtil.isGooglePlayServicesAvailable(
this
),
171.
this
,
0
);
172.
}
173.
}
174.
175.
private
void
setUpMapIfNeeded() {
176.
177.
if
(mMap ==
null
) {
178.
179.
mMap = ((SupportMapFragment) getSupportFragmentManager()
180.
.findFragmentById(R.id.map)).getMap();
181.
182.
183.
if
(mMap !=
null
) {
184.
setUpMap();
185.
}
186.
}
187.
}
188.
189.
190.
191.
public
void
Onclick(View v) {
192.
193.
194.
Context g1 = getApplicationContext();
195.
String g2 =
"คุณอยู่ในคณะวิทยาศาสตร์"
;
196.
int
g3 = Toast.LENGTH_LONG;
197.
Toast g = Toast.makeText(g1, g2, g3);
198.
199.
200.
Context t1 = getApplicationContext();
201.
String t2 =
"คุณไม่ได้อยู่ในคณะวิทยาศาสตร์"
;
202.
int
t3 = Toast.LENGTH_LONG;
203.
Toast t = Toast.makeText(t1, t2, t3);
204.
205.
Context a1 = getApplicationContext();
206.
String a2 =
"คุณอยู่ในคณะเทคโนโลยีการเกษตร"
;
207.
int
a3 = Toast.LENGTH_LONG;
208.
Toast a = Toast.makeText(a1, a2, a3);
209.
210.
Context b1 = getApplicationContext();
211.
String b2 =
"คุณไม่ได้อยู่ในคณะเทคโนโลยีการเกษตร"
;
212.
int
b3 = Toast.LENGTH_LONG;
213.
Toast b = Toast.makeText(b1, b2, b3);
214.
215.
Context c1 = getApplicationContext();
216.
String c2 =
"คุณอยู่ในคณะสถาปัตยกรรมศาสตร์"
;
217.
int
c3 = Toast.LENGTH_LONG;
218.
Toast c = Toast.makeText(c1, c2, c3);
219.
220.
Context d1 = getApplicationContext();
221.
String d2 =
"คุณไม่ได้อยู่ในคณะสถาปัตยกรรมศาสตร์"
;
222.
int
d3 = Toast.LENGTH_LONG;
223.
Toast d = Toast.makeText(d1, d2, d3);
224.
225.
Context e1 = getApplicationContext();
226.
String e2 =
"คุณอยู่ในคณะวิศวะกรรมศาสตร์"
;
227.
int
e3 = Toast.LENGTH_LONG;
228.
Toast e = Toast.makeText(e1, e2, e3);
229.
230.
Context f1 = getApplicationContext();
231.
String f2 =
"คุณไม่ได้อยู่ในคณะวิศวะกรรมศาสตร์"
;
232.
int
f3 = Toast.LENGTH_LONG;
233.
Toast f = Toast.makeText(f1, f2, f3);
234.
235.
236.
}
237.
238.
239.
private
void
setUpMap() {
240.
241.
242.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new
LatLng(
13.729468
,
100.779361
),
16
));
243.
mMap.setIndoorEnabled(
false
);
244.
mMap.setMyLocationEnabled(
true
);
245.
246.
mMap.setOnCameraChangeListener(
this
);
247.
248.
}
249.
250.
251.
252.
@Override
253.
public
void
onCameraChange(CameraPosition position) {
254.
255.
256.
257.
258.
for
(
int
i =
0
; i < mGeofenceCoordinates.size(); i++)
259.
{
260.
261.
mMap.addCircle(
new
CircleOptions()
262.
.center(mGeofenceCoordinates.get(i))
263.
.radius(mGeofenceRadius.get(i).intValue())
264.
.fillColor(
0x40ff0000
)
265.
.strokeColor(Color.TRANSPARENT)
266.
.strokeWidth(
2
));
267.
268.
269.
}
270.
271.
272.
}
273.
274.
275.
}