Commit 8fa36c23 authored by Corentin Mors's avatar Corentin Mors
Browse files

Display users position on Map (need to be enhanced)

parent a9f48eed
......@@ -350,6 +350,7 @@ public class ChatFragment extends Fragment {
}
};
private Emitter.Listener onUserJoined = new Emitter.Listener() {
@Override
public void call(final Object... args) {
......
......@@ -18,11 +18,14 @@ import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
......@@ -31,8 +34,13 @@ import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Marker;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends AppCompatActivity {
......@@ -40,6 +48,8 @@ public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
private String mPassword;
MapView map = null;
GeoPoint currentLocation;
......@@ -91,7 +101,7 @@ public class MainActivity extends AppCompatActivity {
final String serverAddress = settings.getString("serverAddress", null);
final String mUsername = settings.getString("mUsername", null);
final String mPassword = settings.getString("mPassword", null);
mPassword = settings.getString("mPassword", null);
if (serverAddress == null || mUsername == null || mPassword == null) {
Intent intent = new Intent(getBaseContext(), SettingsActivity.class);
startActivity(intent);
......@@ -125,19 +135,22 @@ public class MainActivity extends AppCompatActivity {
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(9.5);
mapController.setZoom(9.0);
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
// Example marker
/*
Marker startMarker = new Marker(map);
startMarker.setPosition(startPoint);
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
*/
ChatApplication app = (ChatApplication) getApplication();
mSocket = app.getSocket();
mSocket.on("geoloc", onGeoloc);
Button settingsButton = findViewById(R.id.accessSettings);
......@@ -156,7 +169,7 @@ public class MainActivity extends AppCompatActivity {
Marker locationMarker = new Marker(map);
locationMarker.setPosition(currentLocation);
locationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
locationMarker.setIcon(getResources().getDrawable(R.drawable.person));
locationMarker.setIcon(getResources().getDrawable(R.drawable.osm_ic_follow_me));
locationMarker.setTitle("Your location");
map.getOverlays().add(locationMarker);
......@@ -164,6 +177,65 @@ public class MainActivity extends AppCompatActivity {
}
private Map<String, Marker> userMarkers = new HashMap<String, Marker>();
public void moveParticipant(String username, String coordinates){
Log.i("OM_GEO", coordinates);
coordinates = coordinates.replace("]", "");
coordinates = coordinates.replace("[", "");
String[] split = coordinates.split(",");
Location uLocation = new Location(username);
uLocation.setLatitude(Double.parseDouble(split[1]));
uLocation.setLongitude(Double.parseDouble(split[0]));
GeoPoint uGeoPoint = new GeoPoint(uLocation);
//uGeoPoint.setCoords(uLocation.getLatitude(), uLocation.getLongitude());
if(userMarkers.containsKey(username)){
userMarkers.remove(username);
}
Marker locationMarker = new Marker(map);
locationMarker.setPosition(uGeoPoint);
locationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);
locationMarker.setIcon(getResources().getDrawable(R.drawable.person));
locationMarker.setTitle(username);
userMarkers.put(username, locationMarker);
map.getOverlays().add(locationMarker);
}
private Emitter.Listener onGeoloc = new Emitter.Listener() {
@Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
String coordinates;
try {
username = data.getString("username");
coordinates = data.getString("coordinates");
} catch (JSONException e) {
Log.e("OM_LOG", e.getMessage());
return;
}
try {
moveParticipant(username, CryptoFunc.sdecrypt(mPassword, coordinates));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
public void onResume(){
super.onResume();
//this will refresh the osmdroid configuration on resuming.
......@@ -204,7 +276,7 @@ public class MainActivity extends AppCompatActivity {
currentLocation.setCoords(loc.getLatitude(), loc.getLongitude());
displayMyCurrentLocationOverlay();
try {
mSocket.emit("geoloc", CryptoFunc.sencrypt("azerty", "["+loc.getLongitude()+","+loc.getLatitude()+"]"));
mSocket.emit("geoloc", CryptoFunc.sencrypt(mPassword, "["+loc.getLongitude()+","+loc.getLatitude()+"]"));
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -2702,7 +2702,7 @@ $(function() {
 
var view = new View({
center: [266736 ,5955932],
zoom: 5
zoom: 2
});
 
var map = new Map({
......@@ -2772,14 +2772,15 @@ $(function() {
 
 
geolocation.on('change:position', function() {
map.getView().setCenter(geolocation.getPosition());
// Set center of the map (may be needed)
//map.getView().setCenter(geolocation.getPosition());
 
var coordinates = geolocation.getPosition();
positionFeature.setGeometry(coordinates ?
new Point(coordinates) : null);
console.log(coordinates);
//console.log(coordinates);
var newCoord = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326');
console.log(newCoord);
//console.log(newCoord);
socket.emit('geoloc', oencrypt(newCoord, password));
});
 
......
......@@ -328,7 +328,7 @@ $(function() {
var view = new View({
center: [266736 ,5955932],
zoom: 5
zoom: 2
});
var map = new Map({
......@@ -398,14 +398,15 @@ $(function() {
geolocation.on('change:position', function() {
map.getView().setCenter(geolocation.getPosition());
// Set center of the map (may be needed)
//map.getView().setCenter(geolocation.getPosition());
var coordinates = geolocation.getPosition();
positionFeature.setGeometry(coordinates ?
new Point(coordinates) : null);
console.log(coordinates);
//console.log(coordinates);
var newCoord = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326');
console.log(newCoord);
//console.log(newCoord);
socket.emit('geoloc', oencrypt(newCoord, password));
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment