gpt4 book ai didi

java - 如何从 Android map Activity 中的 url 获取 JSON 数据

转载 作者:行者123 更新时间:2023-12-01 10:10:10 27 4
gpt4 key购买 nike

嗨,我想知道如何从 URL 获取 JSON 数据并将其放入 android studio 中的 map Activity 中,这将替换默认位置 syndey?我不知道如何做到这一点,因为大多数在线教程没有意义。

这是我想通过 URL 检索的数据

{"Users":[{"name":"Diaz","lon":"51.1251635","lat":"51.296910"},{"name":"Chris","lon":"51.139409","lat":"51.295825"}}

这是 map Activity 生成的默认代码

import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.support.v4.app.FragmentActivity;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
private GoogleMap nMap;



@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);

}


/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(512.33,11.2333);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydeny));
mMap.getMaxZoomLevel();
}


}

最佳答案

首先,在 map 上显示您当前的位置。您所需要做的就是使用谷歌地图获取您首选位置的坐标。您现在可以使用此值创建 LatLng 对象。您的应用程序当前显示悉尼,因为这是 map 相机所在的位置。您在同一 LatLng 处也有一个标记。

您可以简单地获取要显示的位置的坐标并将其传递到 Map 对象中。

回到您的主要问题,即从 URL 获取 JSON 数据;我可以说这很容易实现,并且根据您所询问的上下文,我可以推断您可能正在尝试查询谷歌地图 API。

像 Retrofit( http://square.github.io/retrofit/ ) 这样的库可以帮助您轻松完成此任务。只是不要忘记从后台线程执行该操作。

使用基本的 HttpURLConnection

public JSONObject queryGoogleDistanceApi(String origin, String destination, String API_KEY_PLACES) throws Exception{
String Url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + origin + "&destinations=" + destination + "&mode=driving&language=en&key=" + API_KEY_PLACES;

HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
URL url = new URL(Url);
Log.d(TAG, Url);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
if (conn != null) {
conn.disconnect();
}

JSONObject object = new JSONObject(jsonResults.toString());
return object;
}

关于java - 如何从 Android map Activity 中的 url 获取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36166268/

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