从Json获取最近的用户位置

时间:2014-08-18 17:05:25

标签: android json location

我是Android新手,我正在创建一个应用程序,以便将最近的地理缓存提供给设备的当前位置。

我有这段代码:

public class MainActivity extends FragmentActivity {
private GoogleMap map;
TextView test;
private ArrayList<LatLng> markers = new ArrayList<LatLng>();

// main code.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    test = (TextView) findViewById(R.id.test);

    String result = JSON
            .getResult("http://www.student.soc.napier.ac.uk/~40053830/CMS/markers.php");

    map = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();
    map.setMyLocationEnabled(true);

    try {
        JSONArray jArray = new JSONArray(result);

        for (int j = 0; j < jArray.length(); j++) {
            JSONObject json_data = jArray.getJSONObject(j);
            LatLng tmp = new LatLng(Double.parseDouble(json_data
                    .getString("latitude")), Double.parseDouble(json_data
                    .getString("longitude")));
            map.addMarker(new MarkerOptions().position(tmp).title(
                    json_data.getString("name")));
            markers.add(tmp);
        }
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
    Location myLocation = map.getMyLocation();
    Location nearest = new Location("nearest");
    nearest.setLatitude(markers.get(0).latitude);
    nearest.setLongitude(markers.get(0).longitude);
    float distance = myLocation.distanceTo(nearest);
    for (int i = 1; i < markers.size(); i++) {
        Location target = new Location("target");
        target.setLatitude(markers.get(i).latitude);
        target.setLongitude(markers.get(i).longitude);

        if (myLocation.distanceTo(target) < distance) {
            nearest = target;
            distance = myLocation.distanceTo(target);
        }
    }
    test.setText((String.format("%.02f", distance * 0.000621371192)));
}

我似乎无法在textview上找到距离最近的距离..

这是logcat:

08-18 19:01:50.463: E/AndroidRuntime(16421): FATAL EXCEPTION: main
08-18 19:01:50.463: E/AndroidRuntime(16421): Process:         com.finlaysmith.MotorcycleParking, PID: 16421
08-18 19:01:50.463: E/AndroidRuntime(16421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.finlaysmith.MotorcycleParking/com.finlaysmith.MotorcycleParking.MainActivity}: java.lang.NullPointerException
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.os.Looper.loop(Looper.java:136)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.ActivityThread.main(ActivityThread.java:5001)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at java.lang.reflect.Method.invokeNative(Native Method)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at java.lang.reflect.Method.invoke(Method.java:515)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at dalvik.system.NativeStart.main(Native Method)
08-18 19:01:50.463: E/AndroidRuntime(16421): Caused by: java.lang.NullPointerException
08-18 19:01:50.463: E/AndroidRuntime(16421):    at com.finlaysmith.MotorcycleParking.MainActivity.onCreate(MainActivity.java:66)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.Activity.performCreate(Activity.java:5231)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-18 19:01:50.463: E/AndroidRuntime(16421):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

我知道这是“浮动距离..但我找不到如何解决它..

由于

1 个答案:

答案 0 :(得分:1)

map.myLocation返回null,因为它尚未找到您的位置。这可能需要一些时间。如果返回值为null,则需要检查null并且不执行此部分代码(一旦有了位置,就可以稍后执行)。