Continously updating marker on running Google Map v2
-
18-10-2022 - |
Question
I am making a tracking app that receive data(longitude and latitude) from the user via SMS and display on the googlemapv2. I want my application to work continously and the marker update on new location when a new message is received.But the marker doesn't move to new location. I have made 2 java files. One is "IncomingSms" that receives new SMS and other is "MainActivity" that display google map and show marker.It show marker on defaultposition but don't update on new coordinates. Please help me...here is my code..
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);
}
}
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow