1

我试图实现自动右缩放级别以显示所有标记。但是,我得到空指针异常。这是我的代码:

public class LocationMarkers extends FragmentActivity {
    private static GoogleMap supportMap;
    private ArrayList<LatLng> gp = new ArrayList<LatLng>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_marker);
        locationMarkers();

    }

    public void locationMarkers()
    {
        /*Implement Location Markers*/
        BufferedReader jsonReader = new BufferedReader(new InputStreamReader(this.getResources().openRawResource(R.raw.map)));
        StringBuilder jsonBuilder = new StringBuilder();
        try {
            for (String line = null; (line = jsonReader.readLine()) != null;) {
                jsonBuilder.append(line).append("\n");
            }

            JSONTokener tokener = new JSONTokener(jsonBuilder.toString());
            JSONArray jsonArray = new JSONArray(tokener);


            for (int index = 0; index < jsonArray.length(); index++) 
            {

                JSONObject jsonObject = jsonArray.getJSONObject(index);
                double getLat = jsonObject.getJSONObject("point").getDouble("lat");
                double getLng = jsonObject.getJSONObject("point").getDouble("long");
                String name   = jsonObject.getString("name");
                LatLng myLoc = new LatLng(getLat, getLng);
                gp.add(myLoc);
                Log.d("Long", Double.toString(getLng));             
                FragmentManager fmanager = getSupportFragmentManager();
                Fragment fragment = fmanager.findFragmentById(R.id.map);
                SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
                supportMap = supportmapfragment.getMap();
                    if(supportMap!=null){
                        Marker LocMarker = supportMap.addMarker(new MarkerOptions().position(myLoc)
                                  .title(name));

                        }

            }
              LatLngBounds.Builder builder = new LatLngBounds.Builder();
                for(LatLng m : gp) {
                    Log.e("Tag",Double.toString(m.latitude));
                    builder = builder.include(m);
                    }
                LatLngBounds bounds = builder.build();
                int padding = 0;
                CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
                supportMap.moveCamera(cu);
        } catch (FileNotFoundException e) {
            Log.e("jsonFile", "file not found");
        } catch (IOException e) {
            Log.e("jsonFile", "ioerror");
        } catch (JSONException e) {
            Log.e("jsonFile", "error while parsing json");
        }

    }
}

我也收到另一个警告:Google Play services out of date. Requires 2012100 but found 1015我会很高兴,如果你们帮助找到问题?

4

1 回答 1

4

1)希望您在不在模拟器中的设备上进行测试。您可能会在模拟器中遇到错误,如果您仍想在模拟器上进行测试,请对其进行评论。它将完美地在设备中工作

2)您的 Google Play 服务已过时,因为您的 google play 服务库版本与您的模拟器 google play 服务不同。你必须安装更新的 gms 和 vending apk 文件。

gms:http: //uploaded.net/file/bnzl1si4

自动售货: http ://www.androidfilehost.com/?fid=9390135922294521859

希望它会帮助你。

于 2013-06-19T10:24:35.020 回答