如何从地图 activity 中的 url 获取 JSON 数据 android
How to get JSON data from a url in a map activity for android
你好,我想知道如何从 URL 中获取 JSON 数据并将其放入 android 工作室的地图 activity 中,这将取代默认位置,哪个是syndey?我不知道该怎么做,因为大多数在线教程都没有意义。
这是我要通过 URL
检索的数据
{"Users":[{"name":"Diaz","lon":"51.1251635","lat":"51.296910"},{"name":"Chris","lon":"51.139409","lat":"51.295825"}}
这是地图生成的默认代码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();
}
}
首先,在地图上显示您当前的位置。您需要做的就是使用 google 地图获取您首选位置的坐标。您现在可以使用此值创建 LatLng 对象。
您的应用程序当前显示 sidney,因为那是地图相机所在的位置。您在同一个 LatLng 也有一个标记。
您可以简单地获取要显示的任何位置的坐标,并将其传递到 Map 对象中。
回到您的主要问题,即从 URL 获取 JSON 数据;我可以说这很容易实现,并且根据您询问的上下文,我可以推断您可能正在尝试查询 google 映射 api.
像 Retrofit(http://square.github.io/retrofit/) 这样的库可以帮助您轻松完成这项工作。
只是不要忘记从后台线程执行操作。
使用基本的 HttpURL连接
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;
}
您必须在 onMapReady 方法中调用 api。然后解析它并根据您的数据设置标记。像下面的代码:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//call your api. get son data
//{"Users":[{"name":"Diaz","lon":"51.1251635","lat":"51.296910"},{"name":"Chris","lon":"51.139409","lat":"51.295825"}}
// Add a marker from JSON data, and move the camera.
LatLng diaz = new LatLng(51.296910,51.1251635);
mMap.addMarker(new MarkerOptions().position(diaz).title("Diaz"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(diaz));
mMap.getMaxZoomLevel();
}
我认为您可以解析 Json 数据。如果您在解析 Json 数据时遇到问题,我可以再次帮助您。
你好,我想知道如何从 URL 中获取 JSON 数据并将其放入 android 工作室的地图 activity 中,这将取代默认位置,哪个是syndey?我不知道该怎么做,因为大多数在线教程都没有意义。
这是我要通过 URL
检索的数据{"Users":[{"name":"Diaz","lon":"51.1251635","lat":"51.296910"},{"name":"Chris","lon":"51.139409","lat":"51.295825"}}
这是地图生成的默认代码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();
}
}
首先,在地图上显示您当前的位置。您需要做的就是使用 google 地图获取您首选位置的坐标。您现在可以使用此值创建 LatLng 对象。 您的应用程序当前显示 sidney,因为那是地图相机所在的位置。您在同一个 LatLng 也有一个标记。
您可以简单地获取要显示的任何位置的坐标,并将其传递到 Map 对象中。
回到您的主要问题,即从 URL 获取 JSON 数据;我可以说这很容易实现,并且根据您询问的上下文,我可以推断您可能正在尝试查询 google 映射 api.
像 Retrofit(http://square.github.io/retrofit/) 这样的库可以帮助您轻松完成这项工作。 只是不要忘记从后台线程执行操作。
使用基本的 HttpURL连接
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;
}
您必须在 onMapReady 方法中调用 api。然后解析它并根据您的数据设置标记。像下面的代码:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//call your api. get son data
//{"Users":[{"name":"Diaz","lon":"51.1251635","lat":"51.296910"},{"name":"Chris","lon":"51.139409","lat":"51.295825"}}
// Add a marker from JSON data, and move the camera.
LatLng diaz = new LatLng(51.296910,51.1251635);
mMap.addMarker(new MarkerOptions().position(diaz).title("Diaz"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(diaz));
mMap.getMaxZoomLevel();
}
我认为您可以解析 Json 数据。如果您在解析 Json 数据时遇到问题,我可以再次帮助您。