Sudhanshu Gupta
Sudhanshu Gupta

Reputation: 2315

How to send my location (longitude and latitude) as a reply sms to other person?

I have an application in which I need to send my location(longitude and latitude ) to other person on receiving a sms from him.I'll somehow get my location(longitude and latitude) and put it as a text in reply sms and then send it to other person.But now I'm facing a problem in how to get my location and put it as a text in reply sms.Till now I have written a code which sends a reply message on receiving a sms from other person.Can anyone tell me how to get my location and put it as text message?

Here is my code to send reply message:

public void onReceive(Context context, Intent intent) {

    Intent m=new Intent(context, ReceivelocationActivity.class);    
      PendingIntent pi=PendingIntent.getBroadcast(context, 0, m, 0); 
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = ""; 
    String str2="";
    String str3="";
    String autoReplyToken = "Request_Accepted";
    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str2=msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
         str3=msgs[i].getMessageBody().toString();
            str += "\n";        
        }
        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

        SmsManager sms = SmsManager.getDefault();
        boolean isAutoReply = str3.startsWith(autoReplyToken);
         /* As suggested by Dan J   */
 Criteria hdCrit = new Criteria();
 hdCrit.setAccuracy(Criteria.ACCURACY_COARSE);
 hdCrit.setAltitudeRequired(false);
 hdCrit.setBearingRequired(false);
 hdCrit.setCostAllowed(true);
 hdCrit.setPowerRequirement(Criteria.POWER_LOW);

 hdLocMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

 hdLocProvider = hdLocMgr.getBestProvider(hdCrit, true); 

 Location location = hdLocMgr.getLastKnownLocation(hdLocProvider);

 Double dlat = location.getLatitude();
 Double dlon = location.getLongitude();

        String msg = dlat + "," + dlon ;
/*  As suggested by Dan J  */ 
        if (!isAutoReply) {
            String autoReplyText = autoReplyToken + msg;
            sms.sendTextMessage(str2, null, autoReplyText, pi, null);
        }

    }                 
}

Can anyone tell me how to send actual location in "msg" variable instead of string "location".Anyone with any idea please let me know? Thanks in advance.

Upvotes: 5

Views: 2726

Answers (1)

Dan J
Dan J

Reputation: 25673

See this answer for how to get the longitude and latitude:

Then just change

String msg = "location" ; 

to

String msg = dlat + ", " + dlon; 

You'll also need to add the appropriate permissions to your manifest XML (e.g. see my answer here).

Upvotes: 4

Related Questions