示例#1
0
  @Override
  public View GetView(
      LayoutInflater inflater,
      ViewGroup container,
      SurveyActivity activityRef,
      SurveyAdapter surveyAdapter,
      int parentId) {

    View view = inflater.inflate(R.layout.gps_question, container, false);
    TextView label = (TextView) view.findViewById(R.id.description);
    label.setText(this.label);
    label.setTextSize(fontSize);

    Typeface tf = Typeface.createFromAsset(activityRef.getAssets(), "fonts/OpenSans-Regular.ttf");
    label.setTypeface(tf);

    try {
      label.setTextColor(Color.parseColor(color));
    } catch (Exception e) {
    }

    // SET LABELS
    TextView textlatlabel = (TextView) view.findViewById(R.id.lattext);
    textlatlabel.setText(latLabel);
    textlatlabel.setTypeface(tf);

    TextView textLonlabel = (TextView) view.findViewById(R.id.lontext);
    textLonlabel.setText(lonLabel);
    textLonlabel.setTypeface(tf);

    // GET GPS POSITION
    textLat = (TextView) view.findViewById(R.id.latitude);
    textLon = (TextView) view.findViewById(R.id.longitude);
    textLat.setTypeface(tf);
    textLon.setTypeface(tf);

    if (locationManager == null) {
      // Acquire a reference to the system Location Manager
      locationManager =
          (LocationManager) view.getContext().getSystemService(Context.LOCATION_SERVICE);

      // Define a listener that responds to location updates
      LocationListener locationListener =
          new LocationListener() {
            public void onLocationChanged(Location location) {

              if (LocationUtil.isBetterLocation(location, currentBestLocation)) {
                // Called when a new location is found by the network location provider.
                textLat.setText(String.valueOf(location.getLatitude()));
                textLon.setText(String.valueOf(location.getLongitude()));
                answer =
                    String.valueOf(location.getLatitude())
                        + "/"
                        + String.valueOf(location.getLongitude());

                // Remove the listener you previously added
                locationManager.removeUpdates(this);
              }
            }

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
              // TODO Auto-generated method stub

            }
          };

      // Register the listener with the Location Manager to receive location updates
      locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

      Criteria criteria = new Criteria();
      String provider = locationManager.getBestProvider(criteria, false);
      currentBestLocation = locationManager.getLastKnownLocation(provider);
    }

    // SET ORIENTATION
    LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout);
    if (orientation.equals("horizontal")) layout.setOrientation(LinearLayout.HORIZONTAL);
    else layout.setOrientation(LinearLayout.VERTICAL);

    // SET ANSWER
    if (!answer.equals("")) {
      String[] parts = answer.split("/");
      textLat.setText(parts[0]);
      textLon.setText(parts[1]);
    }
    return view;
  }