Android - Cant get latitude/longitude value to sto

2019-03-04 02:46发布

I am trying to store the latitude/longitude to variables x3 and x4 to pass on Spot[] array but it returns 0. I tried printing it on onConnected method and it works fine.

I also tried other methods to add marker on google map and I put map.addMarker on onConnected method instead but my app closes. If u can suggest where to put addMarker please do. Thanks.

    GoogleMap map;


    private static Location mLastLocation;
    private static GoogleApiClient mGoogleApiClient;

    static double x3;
    static double x4;
    LatLng userloc;



@Override
public void onConnected(Bundle connectionHint) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        x3 = mLastLocation.getLatitude();
        x4 = mLastLocation.getLongitude();
        userloc = new LatLng(x3,x4);

        txtPlateno.setText(String.valueOf(x3+" - "+x4)); // PRINT TEST ONLY WITH CORRECT RESULT

    }
}






private static Spot[] SPOTS_ARRAY = new Spot[]{

    new Spot("YOU ARE HERE", "DESC ", "", new LatLng(x3, x4)), // NOT SHOWN IN MAP AT ALL

            new Spot("Besabella Parking Lot",
                    "Address: Sta Cruz Labogon, Mandaue City\n " +
                            "Fee: \n " +
                            "No. of available space: \n " +
                            "Distance:" + x3 + "KM", "", new LatLng(10.351763, 123.953683)),
            // TESTING x3 ON DISTANCE WITH RESULT 0.0

};

1条回答
在下西门庆
2楼-- · 2019-03-04 03:33

You can call add marker after getting lat/long and also can move camera on that for reference you can check following code

 public class MapDialogFragment extends DialogFragment implements LibListner, OnClickListener, OnInfoWindowClickListener {

private View dialog;
private MarkerOptions markerOptions;
private SupportMapFragment mMapFragment;
private GoogleMap mMap;
private TextView tvBackText, tvback;
// private ArrayList<String> getClickedTitle;
private ArrayList<Data> newData;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Utility.Log("onCreateView");
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    // //
    getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    // getDialog().getWindow()
    // .getAttributes().windowAnimations = R.style.DialogAnimation;
    if (dialog != null) {
        ViewGroup parent = (ViewGroup) dialog.getParent();
        if (parent != null)
            parent.removeView(dialog);
    }
    try {
        dialog = inflater.inflate(R.layout.dlg_map, container, false);
    } catch (Exception e) {
        /* map is already there, just return view as it is */

    }

    return dialog;
}

@Override
public void onStart() {
    super.onStart();
    // Utility.Log("onStart");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Utility.Log("onCreate");
    FragmentManager fm = getChildFragmentManager();
    mMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.mapView);
    if (mMapFragment == null) {
        mMapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.mapView, mMapFragment).commit();
    }
}

@Override
public void onActivityCreated(Bundle arg0) {
    super.onActivityCreated(arg0);

    init();
    loadMap();
    initMap();
    listner();

}

private void loadMap() {
    String map = Utility.BASE_URL + "mall/location";
    new GetLibResponse(MapDialogFragment.this, new LocationGeneralModel(), getActivity(), map, Utility.LOCATIONCOMMON, true, true);

}

@Override
public void onResume() {
    super.onResume();
    try {
        int chkGooglePlayServices = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
        markerOptions = new MarkerOptions();
        if (chkGooglePlayServices != ConnectionResult.SUCCESS) {
            GooglePlayServicesUtil.getErrorDialog(chkGooglePlayServices, getActivity(), 1122).show();
        } else {
            mMap = mMapFragment.getMap();
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        }

        mMap.setMyLocationEnabled(false);
        mMap.setOnInfoWindowClickListener(this);
        // if (isSearch) {
        // mMap.setOnMapClickListener(MapDialogFragment.this);
        // mMap.setOnMapLongClickListener(MapDialogFragment.this);
        // mMap.setOnMarkerDragListener(MapDialogFragment.this);
        //
        // if (latitude != null && longitude != null && latitude != "" && longitude != "") {
        // mMap.clear();
        // markerOptions
        // .position(
        // new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)));
        // mMap.addMarker(markerOptions);
        // mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
        // new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)),
        // 15.0f));
        //
        // }
        // } else {
        // mMap.clear();
        // markerOptions
        // .position(
        // new LatLng(Double.parseDouble(flat), Double.parseDouble(flong)));
        // mMap.addMarker(markerOptions);
        // mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
        // new LatLng(Double.parseDouble(flat), Double.parseDouble(flong)),
        // 15.0f));
        // }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void init() {
    tvback = (TextView) dialog.findViewById(R.id.frg_location_tvBack);
    tvback.setOnClickListener(this);
    tvBackText = (TextView) dialog.findViewById(R.id.frg_location_tvBackText);
    tvBackText.setOnClickListener(this);
}

private void listner() {

}

private void initMap() {

}

private MainFragmentActivity mainActivity() {
    return ((MainFragmentActivity) getActivity());
}

@Override
public void onResponseComplete(Object clsGson, int requestCode) {
    if (requestCode == Utility.LOCATIONCOMMON) {
        if (mainActivity() != null) {
            mainActivity().mLocationGeneralModel = (LocationGeneralModel) clsGson;
            if ((mainActivity().mLocationGeneralModel != null) && (mainActivity().mLocationGeneralModel.data != null)
                    && (mainActivity().mLocationGeneralModel.data.size() != 0)) {
                ArrayList<Double> vLat = new ArrayList<Double>();
                ArrayList<Double> vLong = new ArrayList<Double>();
                ArrayList<String> vName = new ArrayList<String>();
                ArrayList<Data> newData = new ArrayList<LocationGeneralModel.Data>();
                for (int pin = 0; pin < mainActivity().mLocationGeneralModel.data.size(); pin++) {
                    try {
                        if (!mainActivity().mLocationGeneralModel.data.get(pin).vLat.equals("")) {

                            // Log.d("TAG", mainActivity().mLocationGeneralModel.data.get(pin).vLat);
                            vLat.add(Double.parseDouble(mainActivity().mLocationGeneralModel.data.get(pin).vLat));
                            vLong.add(Double.parseDouble(mainActivity().mLocationGeneralModel.data.get(pin).vLong));
                            vName.add((mainActivity().mLocationGeneralModel.data.get(pin).vName_en));
                            double vLatitude[] = new double[vLat.size()];
                            double vLongitude[] = new double[vLong.size()];
                            for (int getArray = 0; getArray < vLat.size(); getArray++) {
                                vLatitude[getArray] = vLat.get(getArray);
                                vLongitude[getArray] = vLong.get(getArray);
                                newData.add(mainActivity().mLocationGeneralModel.data.get(pin));
                            }
                            multipleMarker(vLatitude, vLongitude, vName, newData);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }

        }
    }
}

@Override
public void onResponseError(String errorMessage, int requestCode) {

}

private void multipleMarker(double latitude[], double longitude[], ArrayList<String> vName, ArrayList<Data> newData) {

    this.newData = newData;
    mMap.clear();

    // LatLngBounds.Builder builder = new LatLngBounds.Builder();
    // for (Marker marker : markers) {
    // builder.include(marker.getPosition());
    // }
    // LatLngBounds bounds = builder.build();

    // getClickedTitle = new ArrayList<String>();
    for (int i = 0; i < latitude.length; i++) {
        // Log.d("TAG", "Marker Add" + latitude[i] + " " + longitude[i]);
        MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude[i], longitude[i])).title(vName.get(i));
        mMap.addMarker(marker);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude[i], longitude[i]), 5));
    }
    // getClickedTitle = vName;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.frg_location_tvBack:
        dismiss();
        break;
    case R.id.frg_location_tvBackText:
        dismiss();
        break;
    }
}

@Override
public void onInfoWindowClick(Marker arg0) {

    for (int counter = 0; counter < newData.size(); counter++) {

        if (arg0.getTitle().equals(newData.get(counter).vName_en)) {

            Data data = newData.get(counter);

            MallPerticularsFragment mFragment = new MallPerticularsFragment();
            Bundle bundle = new Bundle();
            bundle.putString("mall_name", data.vName_en);
            bundle.putString("mall_locEn", data.vLocation);

            mFragment.setArguments(bundle);
            ((MainFragmentActivity) getActivity()).displayFragmentWithArg(mFragment);
        }

    }
}
}
查看更多
登录 后发表回答