Rushi K Rushi K - 24 days ago 15
Android Question

How do i show the marker at given position on google map in android?

I am developing an app using google map.In this i want to show marker at location which longitude latitude values fetch form server.How do i show marker at this fetched longitude and latitude value?
Following code fetch the longitude and latitude values from my server successfully, i want to show marker on google map with this fetch langitude and latitude.How do i do?

//java code
public class Location_Track1 extends Activity {

private static final String TAG = "Location_Track1";
private static final long INTERVAL = 1000 * 60 * 1; //1 minute
private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute

private static final float SMALLEST_DISPLACEMENT = 0.25F; //quarter of a meter
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
GoogleMap googleMap;
private ImageView mCurrentPointer;

private double longitude;
private double latitude;
// private ArrayList<LatLng> points; //added
private List<LatLng> points = new ArrayList<>();
Polyline line; //added

TextView tv_mobno, tv_latitude, tv_longitude, tv_time;
String getLatitude;
String getLongitude;
Button slocation;

JSONArray result = null;

ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String CON = "CON";
JSONObject jsonobject;
JSONArray jsonarray;

private String url ="";

private static final String TAG_USER = "result";

// private static final String TAG_SNAME = "pseats";
private static final String TAG_LONG = "longitude";
private static final String TAG_LAT = "latitude";
private static final String TAG_ADDRESS = "paddress";

protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_track);

Log.d(TAG, "onCreate ...............................");

tv_mobno=(TextView)findViewById(R.id.textView_mob);
tv_latitude=(TextView)findViewById(R.id.textView_latitude);
tv_longitude=(TextView)findViewById(R.id.textView_longitude);
tv_time=(TextView)findViewById(R.id.textView_time);
slocation=(Button)findViewById(R.id.button_slocation);

url = "http://example.in/gmap_track.php";

// points = new ArrayList<LatLng>();


googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();


slocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// new DownloadJSON().execute();
new JSONParse().execute();
}
});

}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();

pDialog = new ProgressDialog(Location_Track1.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();

}

@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();

// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {

// Getting JSON Array
result = json.getJSONArray(TAG_USER);
JSONObject c = result.getJSONObject(0);


// Storing JSON item in a Variable
String lat = c.getString(TAG_LAT);
String longt = c.getString(TAG_LONG);


//Set JSON Data in TextView

tv_latitude.setText(lat);
tv_longitude.setText(longt);

/* double lat = c.getDouble(TAG_LAT);
double longt = c.getDouble(TAG_LONG);

MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, longt)).title("point");
googleMap.addMarker(marker);*/


} catch (JSONException e) {
e.printStackTrace();
}

}
}
}

Answer

Why you commented out your code for adding the marker? Try this :

// Storing  JSON item in a Variable
   String lat = c.getString(TAG_LAT);
   String longt = c.getString(TAG_LONG);


//Set JSON Data in TextView

  tv_latitude.setText(lat);
  tv_longitude.setText(longt);

//Parse data to double
  double latD = Double.parseDouble(lat);
  double longtD = Double.parseDouble(longt);

  MarkerOptions marker = new MarkerOptions().position(new LatLng(latD, longtD)).title("point");
  googleMap.addMarker(marker);
Comments