我正在创建一个跟踪应用,通过短信从用户接收数据(经度和纬度)并在googlemapv2上显示。我希望我的应用程序能够连续工作,并在收到新消息时在新位置更新标记。但是标记不会移动到新位置。 我已经制作了2个java文件。一个是接收新短信的“IncomingSms”,另一个是显示谷歌地图和显示标记的“MainActivity”。它在默认位置显示标记,但不在新坐标上更新。 请帮帮我......这是我的代码..
package com.example.chck;
public class MainActivity extends Activity {
public static LatLng point;
GoogleMap gMap;
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
Log.d("Activity","Got new Data again");
Toast.makeText(getApplicationContext(),"In NEW-INTENT", Toast.LENGTH_SHORT).show();
initializeMap();
drawMarker();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.MyMap)).getMap();
initializeMap();
Toast.makeText(getApplicationContext(),"In CREATE", Toast.LENGTH_SHORT).show();
// Enabling MyLocation Layer of Google Map
gMap.setMyLocationEnabled(true);
}
private void drawMarker(){
// Clears all the existing coordinates
String LON="72.99056966";
String LAT="33.64272895";
gMap.clear();
if(IncomingSms.chk==1){
Intent i1 = getIntent();
LAT = i1.getExtras().getString("NewLat");
LON = i1.getExtras().getString("NewLon");
}
Toast.makeText(getBaseContext(),LAT + LON , Toast.LENGTH_SHORT).show();
point = new LatLng(Double.parseDouble(LAT), Double.parseDouble(LON));
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
gMap.clear();
markerOptions.position(point);
// Setting title for the InfoWindow
markerOptions.title("Position");
// Setting InfoWindow contents
markerOptions.snippet("Latitude:"+point.latitude+",Longitude"+point.longitude);
// Adding marker on the Google Map
gMap.addMarker(markerOptions);
// Moving CameraPosition to the user input coordinates
gMap.moveCamera(CameraUpdateFactory.newLatLng(point));
}
private void initializeMap() {
if (gMap == null) {
gMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.MyMap)).getMap();
// check if map is created successfully or not
if (gMap == null)
Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
========================
IncomingSms.java
package com.example.chck;
public class IncomingSms extends BroadcastReceiver {
public static double latitude;
public static double longitude;
public static int chk =0;
public static String la;
public static String lo;
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage
.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage
.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
String[] columns = message.split(",");
assert columns.length == 2;
longitude = Double.parseDouble(columns[0]);
latitude = Double.parseDouble(columns[1]);
la= columns[1];
lo= columns[0];
Log.i("SmsReceiver", "senderNum: " + senderNum
+ "; message: " + message);
int duration = Toast.LENGTH_LONG;
//Toast toast = Toast.makeText(context, "Latitude: "+
//longitude + ", Longitude: " + latitude, duration);
//toast.show();
} // end for loop
chk=1;
//New Location fetched
Toast.makeText(context,la + lo , Toast.LENGTH_SHORT).show();
final Intent i1 = new Intent(context, MainActivity.class);
i1.putExtra("NewLat", la);
i1.putExtra("NewLon", lo);
int duration1 = Toast.LENGTH_LONG;
//Toast toast1 = Toast.makeText(context, "Check Latitude: "+
// lo + ", Longitude: " + la, duration1);
//toast1.show();
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}