【问题标题】:Getting GPS co-ordinates via SMS and show on google map通过短信获取 GPS 坐标并在谷歌地图上显示
【发布时间】:2016-07-25 16:02:31
【问题描述】:

我正在尝试在谷歌地图上显示 GPS 坐标。我正在通过 SMS 获取 GPS 坐标。构建和应用程序生成正确完成,但在真实设备(android kitkat)上启动应用程序后,谷歌地图不显示任何内容,一旦我通过短信收到 GPS 坐标,应用程序崩溃显示“不幸的是地图已停止”。这里是我的代码:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements     
OnMapReadyCallback {

static private GoogleMap mMap;
int index=0;//index of SMS recieved
int index2=0;
char[] Lat=new char[15];
char[] Lon=new char[15];


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
   @Override
    public void onReceive(Context context, Intent intent) {

    // Get the data (SMS data) bound to intent
        Bundle bundle = intent.getExtras();
     SmsMessage[] msgs = null;

        String str = "";
        String format = bundle.getString("format");

        if (bundle != null)
        {
            Object [] pdus = (Object[]) bundle.get("pdus");

            msgs= new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    String format = myBundle.getString("format");
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                }
               // else {
                 //   messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                //}
                strMessage += messages[i].getOriginatingAddress();
                strMessage += " : ";
                strMessage += messages[i].getMessageBody();
                index=strMessage.indexOf(":");
                index2=strMessage.indexOf(",");
                strMessage.getChars(index,index2,Lat,0);//extracting latitude from Latitude: to ,
                index=strMessage.lastIndexOf(":");//getting last index of : which will be Longitude:
                index2=strMessage.length();//getting the length of string
                strMessage.getChars(index,index2,Lon,0);//extracting longitude from : to last char of string
                strMessage += "\n";
            }

            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
            update_location();
        }
            } // bundle is null
        }
 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng venkateshnagar = new LatLng(20.8781753,15.3481637);
    mMap.addMarker(new MarkerOptions().position(venkateshnagar).title("random location for startup"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(venkateshnagar));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(venkateshnagar,18));
}




 void update_location(){
 double Lat1=Double.parseDouble(Lat.toString());
 double Lon2=Double.parseDouble(Lon.toString());
    LatLng found=new LatLng(Lat1,Lon2);
    mMap.addMarker(new MarkerOptions().position(found).title("this is current location"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(found));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(found,18));
    }
   }

我做错了什么。

【问题讨论】:

  • 将崩溃日志添加到您的问题中。
  • 您的getSMS 接收者必须是public,这很可能是导致收到短信时崩溃的原因。不知道你的地图。
  • @Mike M:如果我将其公开,那么 Manifest 会显示找不到此类。那么如何访问这个类呢?
  • @ADG:实际上由于 PC 问题,我无法在模拟器上测试应用程序,所以我在真实设备上使用它。那么真实设备的 crash Log 是如何显示的呢?
  • 这没有任何意义。我不知道为什么您的 IDE 会这样告诉您。它必须是明确的public,否则它是protected,并且不能被系统实例化,这就是你的堆栈跟踪会告诉你的。添加 public 修饰符后尝试清理/重建。 (顺便说一句,@和用户名之间不能有空格,否则他们不会收到消息。我正好回来查看。)

标签: android google-maps gps sms


【解决方案1】:

现在它的工作。我在程序的update_location() 方法中使用了MarkerOptions options。然后通过使用options.position(found)这是更新的方法:

public void update_Loc()
   {                                               // Instantiating MarkerOptions class
    MarkerOptions options = new MarkerOptions();
   // Toast.makeText(getApplicationContext(), Lat.toString()+Lon.toString(), Toast.LENGTH_LONG).show();
    double Lat1=Double.parseDouble(Lat);
    double Lon2=Double.parseDouble(Lon);
    LatLng found=new LatLng(Lat1,Lon2);
    options.position(found);
    Marker mapMarker=mMap.addMarker(options);
    options.title("This is the current location");
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(found,26));
}

我希望其他像我一样尝试申请的人。

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    相关资源
    最近更新 更多