如何从Android的地图活动中的URL获取JSON数据

时间:2016-03-22 22:15:27

标签: java android json geojson

您好我想知道如何从URL获取JSON数据并将其放入android studio中的地图活动中,这将取代默认位置,即syndey?我无法弄清楚如何做到这一点,因为大多数在线教程都没有意义。

这是我想通过网址

检索的数据
{"Users":[{"name":"Diaz","lon":"51.1251635","lat":"51.296910"},{"name":"Chris","lon":"51.139409","lat":"51.295825"}}

这是地图活动

生成的默认代码
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();
    }


}

2 个答案:

答案 0 :(得分:1)

首先,在地图上显示您当前的位置。您需要做的就是使用谷歌地图获得您首选位置的坐标。您现在可以使用此值创建LatLng对象。 您的应用当前显示sidney,因为这是地图相机的位置。您在同一个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;
}

答案 1 :(得分:0)

你必须在onMapReady方法中调用你的api。然后解析它并从数据中设置标记。如下代码:

<script src="https://code.jquery.com/jquery-2.2.2.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>

<div class="draggable item1">
  Drag me!
</div>

<div class="draggable item2">
  Drag me too!
</div>

<div id="div1" class="dropTarget">
  Drag the box here first...
</div>
<div id="div2" class="dropTarget">
  Then drag it here :)
</div>

我认为您可以解析Json数据。如果您在解析Json数据时遇到问题,我可以再次帮助您。