1
1
package com .teamfire .picontroller ;
2
2
3
+ import android .app .Activity ;
3
4
import android .content .Context ;
4
5
import android .content .Intent ;
6
+ import android .content .SharedPreferences ;
5
7
import android .preference .PreferenceManager ;
8
+ import android .support .v4 .content .ContextCompat ;
6
9
import android .support .v7 .app .AppCompatActivity ;
7
10
import android .os .Bundle ;
8
11
import android .view .View ;
9
- import android .view .WindowManager ;
10
12
import android .webkit .WebView ;
11
13
import android .webkit .WebViewClient ;
12
14
import android .widget .Button ;
13
15
import android .widget .EditText ;
14
16
15
17
import org .osmdroid .api .IMapController ;
16
18
import org .osmdroid .config .Configuration ;
19
+ import org .osmdroid .events .MapEventsReceiver ;
17
20
import org .osmdroid .tileprovider .tilesource .TileSourceFactory ;
18
21
import org .osmdroid .util .GeoPoint ;
19
22
import org .osmdroid .views .MapView ;
20
-
23
+ import org .osmdroid .views .overlay .MapEventsOverlay ;
24
+ import org .osmdroid .views .overlay .Marker ;
21
25
22
26
public class AutoDriveActivity extends AppCompatActivity {
23
27
24
- Button btn_manualDrive , btn_camera ;
25
- WebView wb_liveFeed ;
26
- EditText ipAddress ;
27
- MapView map = null ;
28
28
public static String wifiModuleIP ;
29
29
public static int wifiModulePort ;
30
30
public String newUrl ;
31
+ Button btn_manualDrive , btn_camera , btn_showCoordinates , btn_GPS ;
32
+ WebView wb_liveFeed ;
33
+ EditText ipAddress , mapLat , mapLon ;
34
+ MapView map = null ;
31
35
32
36
@ Override
33
37
protected void onCreate (Bundle savedInstanceState ) {
34
38
super .onCreate (savedInstanceState );
35
39
setContentView (R .layout .activity_autodrive );
36
- this .getWindow ().setSoftInputMode (WindowManager .LayoutParams .SOFT_INPUT_STATE_ALWAYS_HIDDEN );
37
40
38
41
btn_manualDrive = findViewById (R .id .btn_manualDrive );
39
- btn_camera = findViewById (R .id .btn_camera2 );
40
- ipAddress = findViewById (R .id .ipAddress2 );
41
- wb_liveFeed = findViewById (R .id .wb_liveFeed2 );
42
-
42
+ btn_camera = findViewById (R .id .btn_camera );
43
+ btn_showCoordinates = findViewById (R .id .btn_ShowCoordinates );
44
+ btn_GPS = findViewById (R .id .btn_GPS );
45
+ mapLat = findViewById (R .id .mapLat );
46
+ mapLon = findViewById (R .id .mapLon );
47
+ ipAddress = findViewById (R .id .ipAddress );
48
+ wb_liveFeed = findViewById (R .id .wb_liveFeed );
43
49
ipAddress .setText (getIntent ().getStringExtra ("IP_ADDRESS" ));
44
50
45
51
btn_camera .setOnClickListener (new View .OnClickListener () {
@@ -58,26 +64,81 @@ public void onClick(View view) {
58
64
btn_manualDrive .setOnClickListener (new View .OnClickListener () {
59
65
@ Override
60
66
public void onClick (View view ) {
61
- setResult (RESULT_OK , new Intent ());
67
+ String enteredIPAddress = ipAddress .getText ().toString ();
68
+ Intent resultIntent = new Intent ();
69
+ resultIntent .putExtra ("IP_ADDRESS_FROM_AUTODRIVE" , enteredIPAddress );
70
+ setResult (Activity .RESULT_OK , resultIntent );
62
71
finish ();
63
72
}
64
73
});
65
74
66
75
Context ctx = getApplicationContext ();
67
76
Configuration .getInstance ().load (ctx , PreferenceManager .getDefaultSharedPreferences (ctx ));
68
77
69
-
70
78
//inflate and create the map
71
79
map = findViewById (R .id .map );
72
80
map .setTileSource (TileSourceFactory .MAPNIK );
73
81
map .setMultiTouchControls (true );
74
82
75
83
//default map view point
76
- IMapController mapController = map .getController ();
77
- mapController .setZoom (7 );
78
- GeoPoint startPoint = new GeoPoint (39.1667 , 35.6667 );
84
+ final IMapController mapController = map .getController ();
85
+ mapController .setZoom (9 );
86
+ final GeoPoint startPoint = new GeoPoint (40.1022 , 26.5167 );
79
87
mapController .setCenter (startPoint );
80
88
89
+ //GPS location marker
90
+ Marker GPSMarker = new Marker (map );
91
+ final GeoPoint markerPointGPS = new GeoPoint (40.15311283875724 , 26.41108989715576 );
92
+ GPSMarker .setPosition (markerPointGPS );
93
+ GPSMarker .setAnchor (Marker .ANCHOR_CENTER , Marker .ANCHOR_BOTTOM );
94
+ GPSMarker .setIcon (ContextCompat .getDrawable (this , R .drawable .gps_marker ));
95
+ GPSMarker .setTitle ("Device Location" );
96
+ map .getOverlays ().add (GPSMarker );
97
+ map .invalidate ();
98
+
99
+ //marker placement
100
+ final Marker startMarker = new Marker (map );
101
+ final MapEventsReceiver mReceive = new MapEventsReceiver () {
102
+ @ Override
103
+ public boolean singleTapConfirmedHelper (GeoPoint p ) {
104
+ mapLat .setText (String .valueOf (p .getLatitude ()));
105
+ mapLon .setText (String .valueOf (p .getLongitude ()));
106
+ GeoPoint markerPoint = new GeoPoint (p .getLatitude (), p .getLongitude ());
107
+ startMarker .setPosition (markerPoint );
108
+ startMarker .setAnchor (Marker .ANCHOR_CENTER , Marker .ANCHOR_BOTTOM );
109
+ startMarker .setTitle ("Target location" );
110
+ map .getOverlays ().add (startMarker );
111
+ map .invalidate ();
112
+ return false ;
113
+ }
114
+
115
+ @ Override
116
+ public boolean longPressHelper (GeoPoint p ) {
117
+ return false ;
118
+ }
119
+ };
120
+ map .getOverlays ().add (new MapEventsOverlay (mReceive ));
121
+
122
+ btn_showCoordinates .setOnClickListener (new View .OnClickListener () {
123
+ @ Override
124
+ public void onClick (View view ) {
125
+ if (mapLat .getText () != null && mapLon .getText () != null ) {
126
+ GeoPoint markerPoint = new GeoPoint (Double .parseDouble (mapLat .getText ().toString ()), Double .parseDouble (mapLon .getText ().toString ()));
127
+ startMarker .setPosition (markerPoint );
128
+ startMarker .setAnchor (Marker .ANCHOR_CENTER , Marker .ANCHOR_BOTTOM );
129
+ startMarker .setTitle ("Target location" );
130
+ map .getOverlays ().add (startMarker );
131
+ map .invalidate ();
132
+ }
133
+ }
134
+ });
135
+
136
+ btn_GPS .setOnClickListener (new View .OnClickListener () {
137
+ @ Override
138
+ public void onClick (View view ) {
139
+ mapController .setCenter (markerPointGPS );
140
+ }
141
+ });
81
142
}
82
143
83
144
public void getIPandPort () {
@@ -87,7 +148,7 @@ public void getIPandPort() {
87
148
wifiModulePort = Integer .valueOf (temp [1 ]);
88
149
}
89
150
90
- public void onResume (){
151
+ public void onResume () {
91
152
super .onResume ();
92
153
//this will refresh the osmdroid configuration on resuming.
93
154
//if you make changes to the configuration, use
@@ -96,12 +157,20 @@ public void onResume(){
96
157
map .onResume (); //needed for compass, my location overlays, v6.0.0 and up
97
158
}
98
159
99
- public void onPause (){
160
+ public void onPause () {
100
161
super .onPause ();
101
162
//this will refresh the osmdroid configuration on resuming.
102
163
//if you make changes to the configuration, use
103
164
//SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
104
165
//Configuration.getInstance().save(this, prefs);
105
166
map .onPause (); //needed for compass, my location overlays, v6.0.0 and up
106
167
}
168
+
169
+ protected void onStop () {
170
+ super .onStop ();
171
+ SharedPreferences .Editor editor = getSharedPreferences ("IP" , MODE_PRIVATE ).edit ();
172
+ editor .putString ("IP" , ipAddress .getText ().toString ());
173
+ editor .commit ();
174
+ }
175
+
107
176
}
0 commit comments