Android 通过短信获取GPS坐标并显示在谷歌地图上
我试图在谷歌地图上显示GPS坐标。我通过短信获取GPS坐标。构建和应用程序生成已正确完成,但在real device(android kitkat)上启动应用程序后,谷歌地图不会显示任何内容,一旦我通过短信收到GPS坐标,应用程序就会崩溃,显示“不幸的是,地图已停止”。以下是我的代码:Android 通过短信获取GPS坐标并显示在谷歌地图上,android,google-maps,gps,sms,Android,Google Maps,Gps,Sms,我试图在谷歌地图上显示GPS坐标。我通过短信获取GPS坐标。构建和应用程序生成已正确完成,但在real device(android kitkat)上启动应用程序后,谷歌地图不会显示任何内容,一旦我通过短信收到GPS坐标,应用程序就会崩溃,显示“不幸的是,地图已停止”。以下是我的代码: import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; i
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));
}
}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Build;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.widget.Toast;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类MapsActivity扩展了FragmentActivity实现
onmapredycallback{
静态私有谷歌地图;
int index=0;//接收到的短信的索引
int index2=0;
char[]Lat=新字符[15];
char[]Lon=新字符[15];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
}
private BroadcastReceiver mReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
//获取绑定到意图的数据(SMS数据)
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
字符串格式=bundle.getString(“格式”);
if(bundle!=null)
{
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];
for(int i=0;i=Build.VERSION\u code.M){
字符串格式=myBundle.getString(“格式”);
messages[i]=SmsMessage.createFromPdu((字节[])PDU[i],格式);
}
//否则{
//messages[i]=SmsMessage.createFromPdu((字节[])PDU[i]);
//}
strMessage+=消息[i]。getOriginatingAddress();
strMessage+=“:”;
strMessage+=messages[i].getMessageBody();
index=strMessage.indexOf(“:”);
index2=strMessage.indexOf(“,”);
strMessage.getChars(index,index2,Lat,0);//从纬度中提取纬度:到,
index=strMessage.lastIndexOf(“:”;//获取:的最后一个索引,该索引将是经度:
index2=strMessage.length();//获取字符串的长度
getChars(index,index2,Lon,0);//从:到字符串的最后一个字符提取经度
strMessage+=“\n”;
}
Toast.makeText(上下文、strMessage、Toast.LENGTH_SHORT).show();
更新_位置();
}
}//bundle为空
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
//在Sydney添加一个标记并移动相机
LatLng-venkateshnagar=新LatLng(20.8781753,15.3481637);
mMap.addMarker(新的MarkerOptions().position(venkateshnagar).title(“启动的随机位置”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(venkateshnagar));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(venkateshnagar,18));
}
无效更新位置(){
double Lat1=double.parseDouble(Lat.toString());
double Lon2=double.parseDouble(Lon.toString());
LatLng found=新LatLng(Lat1,Lon2);
mMap.addMarker(新的MarkerOptions().position(find.title)(“这是当前位置”);
mMap.moveCamera(CameraUpdateFactory.newLatLng(已找到));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(发现,18));
}
}
我做错了什么 现在开始工作了。我在程序的
update\u location()
方法中使用了MarkerOptions选项。然后使用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));
}
我希望其他人也能像我一样尝试应用程序。将崩溃日志添加到您的问题中。您的getSMS
接收器必须是public
,这很可能是收到SMS后导致崩溃的原因。不知道你的地图。@Mike M:如果我公开了它,那么清单显示找不到这个类。那么如何访问这个类?@ADG:实际上由于电脑问题,我无法在emulator上测试这个应用程序,所以我在真实设备上使用它。那么,如何显示真实设备的崩溃日志呢?这没有任何意义。我不知道为什么你的IDE会告诉你这些。它必须显式地公开
,否则它受保护
,并且不能被系统实例化,这就是堆栈跟踪告诉您的。添加public
修改器后,尝试清理/重建。(顺便说一句,@和用户名之间不能有空格,否则他们不会收到消息。我只是碰巧回来检查一下。)