我正在尝试使用标记在Google地图上的MySql中绘制经度纬度值。获取经度纬度值的代码是正确的,因为代码在使用Toast Message时显示这些值。问题是一切都很顺利,除了我在谷歌地图上看不到任何标记。这是我的源代码。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private String JSON_STRING;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
getJSON();
/* LatLng sydney = new LatLng(33, 88);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
*/
}
private void showLongLat(){
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String id = jo.getString(Config.TAG_ID);
String longitude = jo.getString(Config.TAG_LONG);
String latitude = jo.getString(Config.TAG_LAT);
Toast.makeText(getApplicationContext(),latitude+longitude,Toast.LENGTH_LONG).show();
LatLng marker = new LatLng(Double.parseDouble(longitude),Double.parseDouble(latitude));
//LatLng sydney = new LatLng(42+i,87-i);
mMap.addMarker(new MarkerOptions().position(marker).title("Marker Somewhere"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(marker));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MapsActivity.this,"Fetching Data","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showLongLat();
}
@Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(Config.URL_GET_ALL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
}
答案 0 :(得分:1)
您正在更改纬度和经度的值。这一行:
LatLng marker = new LatLng(Double.parseDouble(longitude),Double.parseDouble(latitude));
必须:
LatLng marker = new LatLng(Double.parseDouble(latitude),Double.parseDouble(longitude));