gpt4 book ai didi

android - android 设备上的 map 仅显示来自 url 的一个标记,但在我使用 Toast 时显示所有值

转载 作者:行者123 更新时间:2023-11-29 19:34:24 24 4
gpt4 key购买 nike

我在谷歌上尝试了很多改变,但我的问题没有得到解决。我有一个包含以下数据的 URL。在 map 上,它应该显示多个标记,因为我有 9 个坐标。但它显示了一个标记,其中包含最后一个响应值的坐标。任何帮助都将不胜感激。

{  
"status":200,
"response":[
{
"docId":"1",
"docName":"Madan",
"docMobileNumber":"9676499774",
"location":"S R Nagar",
"specialization":"ENT",
"avaliablity":"Available",
"lat":"17.4436",
"log":"78.4458"
},
{
"docId":"2",
"docName":"Kumar",
"docMobileNumber":"9052598855",
"location":"KPHB",
"specialization":"Pediatrician",
"avaliablity":"Available",
"lat":"17.4948",
"log":"78.3996"
},
{
"docId":"3",
"docName":"charan",
"docMobileNumber":"8080809089",
"location":"Ameerpet",
"specialization":"Dentist",
"avaliablity":"Available",
"lat":"17.4375",
"log":"78.4483"
},
{
"docId":"4",
"docName":"Vamsy",
"docMobileNumber":"7777778888",
"location":"Kukatpally",
"specialization":"Orthopedic",
"avaliablity":"Available",
"lat":"17.4948",
"log":"78.3996"
},
{
"docId":"5",
"docName":"Ganesh",
"docMobileNumber":"9878686544",
"location":"Dilsuk Nagar",
"specialization":"Dermatologist",
"avaliablity":"Available",
"lat":"17.3688",
"log":"78.5247"
},
{
"docId":"6",
"docName":"Savitri",
"docMobileNumber":"8786599452",
"location":" West Marredpally",
"specialization":"Physician",
"avaliablity":"Not Available",
"lat":"17.4500",
"log":"78.5006"
},
{
"docId":"7",
"docName":"Sandhya",
"docMobileNumber":"9873243687",
"location":"Bowenpally",
"specialization":"Eye Specialist",
"avaliablity":"Available",
"lat":"17.898",
"log":"78.5008"
},
{
"docId":"8",
"docName":"Padma",
"docMobileNumber":"9768832418",
"location":"Kompally",
"specialization":"Cardiologist",
"avaliablity":"Not Available",
"lat":"17.5600",
"log":"78.5343"
},
{
"docId":"9",
"docName":"Priya",
"docMobileNumber":"9898767654",
"location":"Tirumalgiri",
"specialization":"Nerphrologist",
"avaliablity":"Available",
"lat":"17.787",
"log":"78.9805"
}
]
}

package com.example.charan.markermap;

import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

public class Maps3 extends FragmentActivity {

private GoogleMap mMap;

ArrayList<HashMap<String, Double>> locationList = new ArrayList<HashMap<String, Double>>();
JSONArray locations = new JSONArray();
int json1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

new TaskRead().execute();
}


public class TaskRead extends AsyncTask<Void, Void, Void> implements OnMapReadyCallback {

ProgressDialog pg;
Double latitude, longitude;

protected void onPreExecute() {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
pg = ProgressDialog.show(Maps3.this, "Please Wait", "Conecting to Server");
pg.setCancelable(true);
}


protected Void doInBackground(Void... params) {
try {

String url = "http://192.168.1.33:8282/DocFinderServices/doctorService/doctorsList";

HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream inputStreamResponse = entity.getContent();

String str = convertStreamToString(inputStreamResponse);

if (str != null) {
try {
JSONObject jsonObj = new JSONObject(str);

// Getting JSON Array node
locations = jsonObj.getJSONArray("response");
json1 = jsonObj.getInt("status");

// looping through All records
for (int i = 0; i < locations.length(); i++) {
JSONObject c = locations.getJSONObject(i);

latitude = c.getDouble("lat");
longitude = c.getDouble("log");

// tmp hashmap for single contact
HashMap<String, Double> location = new HashMap<>();

// adding each child node to HashMap key => value
location.put("latitude", latitude);
location.put("longitude", longitude);
locationList.add(location);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

protected void onPostExecute(Void gmap) {
mMap.clear();
Marker[] allMarkers = new Marker[locationList.size()];
Toast.makeText(Maps3.this, "The list size is " + locationList.size(), Toast.LENGTH_SHORT).show();

for (int i = 0; i < locationList.size(); i++) {

if (mMap != null && json1 == 200) {
for (int j = 0; j < locationList.size(); j++) {
LatLng latLng = new LatLng(latitude, longitude);
allMarkers[i] = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ok)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.0f));
mMap.getUiSettings().isZoomGesturesEnabled();
}

} else {
Toast.makeText(Maps3.this, "Oops..their is no map........", Toast.LENGTH_SHORT).show();
}
}
pg.dismiss();

}

public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

}
}
}

最佳答案

在收到回调 onMapReady() 后尝试执行 TaskRead

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Set Zoom Controls and Gestures on map
mMap.getUiSettings().setZoomControlsEnabled(true)
mMap.getUiSettings().setZoomGesturesEnabled(true)

new TaskRead().execute();
}

FragmentActivity 中实现 OnMapReadyCallback 并在 onCreate 方法中初始化您的 SupportMapFragment,如下所示。

public class Maps3 extends FragmentActivity implements OnMapReadyCallback {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

}

尝试在 map 上设置缩放控件和手势,然后尝试放大或缩小,看看 map 上是否添加了其他标记?

检查我更新的 onMapReady 方法。

关于android - android 设备上的 map 仅显示来自 url 的一个标记,但在我使用 Toast 时显示所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39424443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com