我试图解决这整天,但我不能,所以我得请你帮忙。我的应用程序是监视手机从基站接收的数据,并通过PHP和JSON从数据库中获取数据,并在地图上绘制标记(这很好)。如何在同一张地图上添加我的位置标记?我希望该标记与LocationChange一起使用,以便用户可以从数据库中查看他与标记的位置关系。此外,如果您在代码中看到任何更好的解决方案,请告诉我。谢谢。
这是一段代码:
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);
}
}
}