我正在填充带有标记的Google地图,我没有收到错误,我的变量正在获取它们的值,但它不会填充任何标记。它只显示一个标记,它是我当前的位置。
这是填充时调用的代码:
private void showData() {
int count = users.length();
for(;i<=count;i++) {
try {
JSONObject jsonObject = users.getJSONObject(i);
maplat = Double.parseDouble(jsonObject.getString(lat));
maplng = Double.parseDouble(jsonObject.getString(lng));
MarkerOptions marker = new MarkerOptions().position(new LatLng(maplat, maplng)).title("Hello Maps");
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
googleMap.addMarker(marker);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
我的整个代码:
public class MapActivity extends AppCompatActivity{
private GoogleMap googleMap;
double curLat;
double curLng;
double maplat;
double maplng;
private static final String JSON_ARRAY = "result";
private static final String lat= "lat";
private static final String lng = "lng";
private JSONArray users = null;
private String q;
private static final String GET_URL = "http://testevent.site88.net/getlocation.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
GPSTracker gps = new GPSTracker(this);
curLat = gps.getLatitude();
curLng = gps.getLongitude();
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
private void extractJSON(){
try {
JSONObject jsonObject = new JSONObject(q);
users = jsonObject.getJSONArray(JSON_ARRAY);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void showData() {
int count = users.length();
for(int i = 0;i<=count;i++) {
try {
JSONObject jsonObject = users.getJSONObject(i);
maplat = Double.parseDouble(jsonObject.getString(lat));
maplng = Double.parseDouble(jsonObject.getString(lng));
MarkerOptions marker = new MarkerOptions().position(new LatLng(maplat, maplng)).title("Hello Maps");
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
googleMap.addMarker(marker);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private void getJSON(String url) {
class GetJSON extends AsyncTask<String, Void, String>{
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MapActivity.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){
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
q=s;
extractJSON();
showData();
MarkerOptions marker = new MarkerOptions().position(new LatLng(curLat, curLng)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin));
// adding marker
googleMap.addMarker(marker);
}
}
GetJSON gj = new GetJSON();
gj.execute(url);
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
getJSON(GET_URL);
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}