Notice
Recent Posts
Recent Comments
Today
Total
05-02 00:04
Archives
관리 메뉴

Jeongchul Kim

Google Map API 이용 라이딩(거리,속도)정보 SharedPreferences 저장 웹서버의 MySQL DB 전송 - 1. XML 본문

Android

Google Map API 이용 라이딩(거리,속도)정보 SharedPreferences 저장 웹서버의 MySQL DB 전송 - 1. XML

김 정출 2016. 3. 15. 15:35

Google Map API 이용 라이딩(거리,속도)정보 SharedPreferences 저장 웹서버의 MySQL DB 전송 - 1. XML


이전 포스트 필히 참고!

http://jungchul.tistory.com/310  Google Map Fragment 만들기



전체 개략도

전체적인 개략도입니다.
주행시간과 주행거리와 평균속도를 실시간으로 보여주고

통계파트에서 최근 주행에 대한 기록을 보여주며, 누적되는 합계 계산도 보여줍니다.



Start 버튼을 눌러 시작 지점을 지정하며 타이머가 작동됩니다.

매 5초마다 handler를 발생하여 gps의 지점을 찍습니다.

Stop 버튼은 타이머를 멈추면 통계가 끝납니다.

Reset 버튼은 모든 변수를 (타이머를 0)으로 초기화합니다.

통계 버튼은 최종적으로 Fragment를 이동하여 시작 지점과 종료 지점을 통해 라이딩에 대한 통계를 이루어집니다.


설계를 해보겠습니다.

1. 변수 지정

거리 > 시작 지점, 종료 지점, 총 라이딩 거리

시간 > 시작 시간, 종료 시간, 총 라이딩 시간

속력 > 순간 속도(실시간),평균 속도(라이딩 거리/라이딩 시간)

총 8개


2. 설계

Start 버튼의 OnClickListener의 동작은 시작 지점을 등록 합니다.

이를 통해 시작 지점, 시작 시간을 뽑을 수 있습니다.

이 후에 1초마다 동작되는 Handler에서 3초마다 GPS를 찍는 부분에서

이전 GPS 지점과 현재 GPS 지점을 통해 라이딩 거리(이를 다 더하면 총 라이딩 거리) 및 순간 속도 (라이딩 거리/5초)를 알 수 있습니다.


Stop 버튼이 눌린다면 타이머를 정지합니다.


Reset 버튼이 눌린다면 DB로 데이터를 전송하고 모든 변수(시작 지점과 종료 지점의 위도, 경도와 타이머 등)를 초기화 해주어야 합니다.


다시 Start 버튼이 눌리려면 Reset 버튼을 눌려야 지만 계산을 진행해야 합니다.


통계 버튼이 눌린다면, 마지막으로 종료 지점의 GPS와 종료 시간 그리고 기존에 카운팅 되던 시간으로 총 라이딩 시간을 알 수 있으며, 시작된 동안 라이딩 거리가 더 해진 총 라이딩 거리를 통해 평균 속도를 알 수 있습니다.


3. DB 저장할 변수

유저의 아이디, 시작 지점 (위도, 경도), 종료 지점 (위도, 경도), 총 라이딩 거리, 총 라이딩 시간, 평균 속도

입니다.

private double sum_dist; // 총 라이딩 거리

private int timer = 0; // 총 라이딩 시간

private double avg_speed;// 평균 속도

private double s_lat; // 시작지점 경도

private double s_long;// 시작지점 위도

private double f_lat;// 종료지점 경도

private double f_long;// 종료지점 위도


4. 라이딩 거리, 평균 속도도 띄워보기


fragment_map2.xml

fragment_map2.xml 수정해봅시다.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:background="#fff"

  android:orientation="vertical" >


  <com.google.android.gms.maps.MapView

      android:id="@+id/map"

      android:name="com.google.android.gms.maps.MapFragment"

      android:layout_width="match_parent"

      android:layout_height="match_parent"

      />


  <LinearLayout

      android:layout_marginBottom="5dp"

      android:id="@+id/fragment_map2_LL_1"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:gravity="center"

      android:orientation="horizontal"

      android:layout_alignParentBottom="true"

      android:layout_centerHorizontal="true"

      >


      <Button

          android:id="@+id/btn_timer_start"

          android:layout_width="80dp"

          android:layout_height="40dp"

          android:layout_marginRight="10dp"

          android:text="Start"

          android:background="@color/color_button"

          android:textColor="@color/color_WHITE"/>


      <Button

          android:id="@+id/btn_timer_finish"

          android:layout_width="80dp"

          android:layout_height="40dp"

          android:layout_marginRight="10dp"

          android:text="Stop"

          android:background="@color/color_button"

          android:textColor="@color/color_WHITE"/>


      <Button

          android:id="@+id/btn_timer_reset"

          android:layout_width="80dp"

          android:layout_height="40dp"

          android:layout_marginRight="10dp"

          android:text="Reset"

          android:background="@color/color_button"

          android:textColor="@color/color_WHITE"

          />


      <Button

          android:id="@+id/btn_riding_for_analysis"

          android:layout_width="100dp"

          android:layout_height="40dp"

          android:text="통계"

          android:background="@color/color_button"

          android:textColor="@color/color_WHITE"

          />

  </LinearLayout>

  <TextView

      android:id="@+id/tv_timer_introduce"

      android:layout_gravity="center"

      android:gravity="center"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:text="라이딩 기록"

      android:textSize="30sp"

      android:textColor="@color/color_MAIN"/>

  <TextView

      android:layout_gravity="center"

      android:gravity="center"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:id="@+id/tv_timer"

      android:textSize="20sp"

      android:layout_below="@id/tv_timer_introduce"

      android:layout_alignParentStart="true"

      android:text="주행시간 : 0 초" />

  <TextView

      android:layout_gravity="center"

      android:gravity="center"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:id="@+id/tv_distance"

      android:textSize="20sp"

      android:layout_below="@id/tv_timer"

      android:layout_alignParentStart="true"

      android:text="주행거리 : 0 m" />

  <TextView

      android:layout_gravity="center"

      android:gravity="center"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:id="@+id/tv_avg_speed"

      android:textSize="20sp"

      android:layout_below="@id/tv_distance"

      android:layout_alignParentStart="true"

      android:text="평균 속도 : 0 m/s" />





</RelativeLayout>


이어서 통계 부분도 xml을 추가해봅시다.

fragment_stats.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical" android:layout_width="match_parent"

  android:layout_height="match_parent">


  <LinearLayout

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

      android:orientation="vertical"

      android:gravity="center"

      android:weightSum="1">

      <TextView

          android:layout_width="match_parent"

          android:layout_height="wrap_content"

          android:text="통계"

          android:layout_marginTop="20dp"

          android:textSize="40sp"

          android:gravity="center_horizontal"

          android:textColor="@color/color_MAIN" />

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1"

          android:layout_marginTop="20dp">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="최근 라이딩 시간 : "

              android:textSize="13sp"

              android:layout_weight="0.22" />

          <TextView

              android:id="@+id/stats_recent_time"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="최근 라이딩 거리 : "

              android:textSize="13sp"

              android:layout_weight="0.29" />

          <TextView

              android:id="@+id/stats_recent_distance"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="최근 라이딩 속도 : "

              android:textSize="13sp"

              android:layout_weight="0.29" />

          <TextView

              android:id="@+id/stats_recent_avg_speed"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="최근 포인트 : "

              android:textSize="13sp"

              android:layout_weight="0.24" />

          <TextView

              android:id="@+id/stats_recent_point"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1"

          android:layout_marginTop="50dp">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="합계 라이딩 시간 : "

              android:textSize="13sp"

              android:layout_weight="0.22" />

          <TextView

              android:id="@+id/stats_total_time"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="합계 라이딩 거리 : "

              android:textSize="13sp"

              android:layout_weight="0.29" />

          <TextView

              android:id="@+id/stats_total_distance"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>

      <LinearLayout

          android:layout_width="match_parent"

          android:layout_height="50dp"

          android:layout_marginLeft="30dp"

          android:weightSum="1">

          <TextView

              android:layout_width="wrap_content"

              android:layout_height="50dp"

              android:text="합계 포인트 : "

              android:textSize="13sp"

              android:layout_weight="0.24" />

          <TextView

              android:id="@+id/stats_total_point"

              android:layout_width="200dp"

              android:layout_height="50dp"

              android:background="@drawable/apptheme_textfield_activated_holo_light"

              />

      </LinearLayout>


  </LinearLayout>

</LinearLayout>



다음 포스트 부터 Fragment 소스를 추가하겠습니다.












Comments