728x90

현재 위치를 문자로 보내기

현재 내 위치를 문자로 상대방에게 보내는 기능을 구현 테스트 해봤다.

2010년대 초반대에 지도가 막 대세로 떠오르던 그 시절에 서비스 기획하면서 내 위치를 상대방에게 보내주면 주변에서 내가 헤메고 있을 때 유용할 거 같았다.

개인정보 등에 민감한 사항이지만 내 위치를 보내주는 것은 괜찮지 않을까 하는 생각이 들었다.

어디까지나 기능 구현 차원에서 접근하는 것이니 이점은 감안하고 참고하시길 ^^


AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- GPS -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.SEND_SMS" />



send_message_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/phoneno"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:singleLine="true"
        android:textSize="16sp"
        android:textColor="#000000" />

</LinearLayout>


https://link2me.tistory.com/1703에서  setCurrentLocation 메소드 부분을 아래와 같이 수정한다.


public void setCurrentLocation(final Location location, String markerTitle, String markerSnippet) {
    if (currentMarker != null) currentMarker.remove();

    LatLng currentLatLng = new LatLng(location.getLatitude(), location.getLongitude());

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(currentLatLng);
    markerOptions.title(markerTitle);
    markerOptions.snippet(markerSnippet);
    markerOptions.draggable(true);

    currentMarker = mMap.addMarker(markerOptions);
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            final View layout = inflater.inflate(R.layout.send_message_popup,null);
            AlertDialog.Builder lms_confirm = new AlertDialog.Builder(mContext);
            lms_confirm.setTitle("발송할 휴대폰번호 등록");
            lms_confirm.setView(layout);
            final EditText etphoneNO = (EditText) layout.findViewById(R.id.phoneno);
            // 확인 버튼 설정
            lms_confirm.setPositiveButton("등록", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String phoneNO = etphoneNO.getText().toString().trim();
                    if(phoneNO.length() == 0) {
                        AlertDialog.Builder phoneNO_confirm = new AlertDialog.Builder(mContext);
                        phoneNO_confirm.setMessage("휴대폰 번호를 입력하세요.").setCancelable(false).setPositiveButton("확인",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) { // 'YES'
                                        dialog.dismiss();
                                    }
                                });
                        AlertDialog alert = phoneNO_confirm.create();
                        alert.show();
                        return;
                    }
                    String gps_location =String.valueOf(location.getLatitude())+","+String.valueOf(location.getLongitude());
                    SMS_Send(phoneNO,gps_location );
                }
            });
            lms_confirm.setNegativeButton("취소", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            lms_confirm.show();
            return true;
        }
    });

    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLng(currentLatLng);
    mMap.moveCamera(cameraUpdate);
}

private void SMS_Send(String phoneNO, String message){ // SmsManager API
    String sms_message = "구글 지도 위치를 보내왔습니다.\n";
    sms_message += "http://maps.google.com/maps?f=q&q="+message+"\n"+"누르면 상대방의 위치를 확인할 수 있습니다.";
    Log.e(TAG,"SMS : "+sms_message);
    try {
        //전송
        SmsManager smsManager = SmsManager.getDefault();
        ArrayList<String> parts = smsManager.divideMessage(sms_message);
        smsManager.sendMultipartTextMessage(phoneNO, null, parts, null, null);
        Toast.makeText(getApplicationContext(), "위치전송 문자보내기 완료!", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}


2019.12.15 일 추가 사항

SMS 보내기가 안된다는 댓글 문의가 있어서 적는다.

위험권한에 해당하는 기능은 별도 처리를 해줘야 한다.

https://www.tutorialspoint.com/android/android_sending_sms.htm 에 나온 내용을 참조하거나, https://link2me.tistory.com/1532 에 설명된 TED 퍼미션 사용법을 이용하면 된다.


도움이 되셨다면 ... 해 주세요. 좋은 글 작성에 큰 힘이 됩니다.

블로그 이미지

Link2Me

,