I was trying to solve this whole day but I cant, so I have to ask you for help. My application is monitoring data that phones receive from base station and also takes data from database through PHP and JSON and draws an marker on the map (this works fine). How can I add my location marker on the same map? I would like that marker is working with LocationChange so that user can see his position relation to the marker from database. Also, if you see any better solutions in the code, pls advise me. Thank you.
Here is a code:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
TextView textCellId, textCellLac, textCellSignalStrength, textCellNetworkMode;
private TextView textCellName;
TelephonyManager Phone;
procGetCellData Listener;
GsmCellLocation location;
String networkmode;
//
private static final String JSON_URL = "http://neo.telenor.rs/telenorapp/fetchdata.php";
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
textCellName=(TextView) findViewById(R.id.textCellName);
textCellName.setMovementMethod(new ScrollingMovementMethod());
textCellId=(TextView) findViewById(R.id.textCellId);
textCellLac=(TextView) findViewById(R.id.textCellLac);
textCellSignalStrength=(TextView) findViewById(R.id.textCellSignalStrength);
textCellNetworkMode=(TextView) findViewById(R.id.textCellNetworkMode);
//
Listener=new procGetCellData();
Phone=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Phone.listen(Listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS | PhoneStateListener.LISTEN_CELL_LOCATION);
}
@Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.setMyLocationEnabled(true);
}
private class procGetCellData extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength){
super.onSignalStrengthsChanged(signalStrength);
//
int SignalStrength_ASU=signalStrength.getGsmSignalStrength();
int SignalStrength_dbm = (2*SignalStrength_ASU)-113;
textCellSignalStrength.setText(String.valueOf(SignalStrength_dbm));
//
String NM = null;
switch (Phone.getNetworkType()) {
case 1:
NM="GPRS";
networkmode = "GSM";
break;
case 2:
NM="EDGE";
networkmode = "GSM";
break;
case 3:
NM="UMTS";
networkmode = "UMTS";
break;
case 8:
NM="HSDPA";
networkmode = "UMTS";
break;
case 9:
NM="HSUPA";
networkmode = "UMTS";
break;
case 10:
NM="HSPA";
networkmode = "UMTS";
break;
case 13:
NM="LTE";
networkmode = "LTE";
break;
case 15:
NM="HSPA+";
networkmode = "UMTS";
break;
case 0:
NM="Unknown";
networkmode = "Unknown";
break;
}
textCellNetworkMode.setText(NM);
}
@Override
public void onCellLocationChanged(CellLocation lokacija) {
super.onCellLocationChanged(lokacija);
location = (GsmCellLocation) Phone.getCellLocation();
textCellId.setText(String.valueOf(location.getCid() % 65536));
textCellLac.setText(String.valueOf(location.getLac()));
String JSON_URL_string=JSON_URL + "?cellid=" + textCellId.getText().toString();
getJSON(JSON_URL_string);
}
private void getJSON(String url) {
class GetJSON extends AsyncTask<String, Void, String> {
ProgressDialog loading;
double lat=0.0;
double log=0.0;
@Override
protected void onPreExecute() {
loading = ProgressDialog.show(MainActivity.this, "Please Wait...", null, true, true);
}
@Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray result = jsonObject.getJSONArray("result");
for (int i=0; i<result.length();i++) {
JSONObject object=result.getJSONObject(i);
String CellName = object.getString("cellname");
String CellAttr = object.getString("cellattr");
if (CellAttr.equals(networkmode) ) {
textCellName.setText(CellName);
String CellLat = object.getString("celllat");
String CellLon = object.getString("celllon");
String CellDir = object.getString("celldir");
double CellLatitude = Double.parseDouble(CellLat);
double CellLongitude = Double.parseDouble(CellLon);
GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
//
MarkerOptions Mojmarker = new MarkerOptions().position(new LatLng(lat, log)).title("Moja lokacija");
map.addMarker(Mojmarker);
//
CameraUpdate cameraUpdate =CameraUpdateFactory.newLatLngZoom(new LatLng(CellLatitude, CellLongitude), 17);
map.animateCamera(cameraUpdate);
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(CellLatitude, CellLongitude)).title(CellName);
marker1.icon(BitmapDescriptorFactory.fromResource(R.drawable.site));
map.addMarker(marker1);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
loading.dismiss();
}
}
GetJSON gj = new GetJSON();
gj.execute(url);
}
}
}