我想通过从 json 获取经度和纬度在谷歌地图上显示标记
Posted
技术标签:
【中文标题】我想通过从 json 获取经度和纬度在谷歌地图上显示标记【英文标题】:i want to show marker on google map by getting longitude and latitude from json 【发布时间】:2016-12-15 06:13:40 【问题描述】:我想通过从 json 获取经度和纬度来在谷歌地图上显示标记。如果我在 AsyncTask 中使用标记,它不会在给定的纬度显示标记... 到目前为止我在这里......提前谢谢......
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
private GoogleMap mMap;
String city = "New Delhi";
String location = null;
double lat;
double lng ;
@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);
new JSONTask().execute("http://www.xxxxxxx.com/bar/mob.php");
@Override
public void onMapReady(GoogleMap googleMap)
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng latLng = new LatLng(-30, 140);
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// i want use that lat and lng here which i am getting in AsyncTask
LatLng latLng1 = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng1).title("Marker"));
mMap.setMyLocationEnabled(true);
private class JSONTask extends AsyncTask<String, String, List<Bardata>>
@Override
protected List<Bardata> doInBackground(String... params)
HttpURLConnection connection = null;
BufferedReader reader = null;
try
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("location", location)
.appendQueryParameter("city", city);
String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
buffer.append(line);
String finalJson = buffer.toString();
System.out.println("Returned value " + finalJson.toString());
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("home");
List<Bardata> bardataList = new ArrayList<>();
Gson gson = new Gson();
for (int i = 0; i < parentArray.length(); i++)
JSONObject finalObject = parentArray.getJSONObject(i);
Bardata barData = gson.fromJson(finalObject.toString(), Bardata.class);
bardataList.add(barData);
lat = barData.getLatitude();
lng = barData.getLongitude();
/* LatLng latLng1 = new LatLng(lat, lng);
mMap.addMarker(new MarkerOptions().position(latLng1).title("Marker"));
mMap.setMyLocationEnabled(true);*/
return bardataList;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
return null;
@Override
protected void onPreExecute()
super.onPreExecute();
@Override
protected void onPostExecute(final List<Bardata> result)
super.onPostExecute(result);
if (result != null)
else
Toast.makeText(getApplicationContext(), "Currently,we are not present in your city", Toast.LENGTH_SHORT).show();
bardata.java
public class Bardata
private String barname;
private double longitude;
private double latitude;
public double getLongitude()
return longitude;
public void setLongitude(double longitude)
this.longitude = longitude;
public double getLatitude()
return latitude;
public void setLatitude(double latitude)
this.latitude = latitude;
public String getBarname()
return barname;
public void setBarname(String barname)
this.barname = barname;
【问题讨论】:
【参考方案1】:对于这个问题,github 上给出了示例代码 试试this
【讨论】:
【参考方案2】:您需要在 UI 线程上添加标记,因此您需要调用方法从 runOnUiThread 中添加标记
for (int i = 0; i < parentArray.length(); i++)
JSONObject finalObject = parentArray.getJSONObject(i);
Bardata barData = gson.fromJson(finalObject.toString(), Bardata.class);
bardataList.add(barData);
lat = barData.getLatitude();
lng = barData.getLongitude();
LatLng latLng1 = new LatLng(lat, lng);
runOnUiThread(new Runnable()
public void run()
try
mMap.addMarker(new MarkerOptions().position(latLng1).title("Marker"));
mMap.setMyLocationEnabled(true);
catch (JSONException e)
Log.e(LOG_TAG, "Error adding marker", e);
);
【讨论】:
logcat 没有错误或警告,但标记未显示。 添加的标记是否与地图初始化的位置相同?在 onMapReady 方法中,您将地图移动到悉尼。以上是关于我想通过从 json 获取经度和纬度在谷歌地图上显示标记的主要内容,如果未能解决你的问题,请参考以下文章
如何通过对vue js中的每个搜索结果使用模态在谷歌地图上显示json数据获得的纬度和经度?