title: Calculating the shortest distance places from my current location and mark the shortest distance places using marker on google map in android
body: <p>I have in my hand the latitude and longitude values of several places in my server. These latitude and longitude values are sent from the server and received at the android mobile successfully. Now I need to get the current location of my android phone and calculate the nearest place to my current location and mark it on the google map. I know how to mark a place on the google map, but I don't know how to calculate distances from my current location to other locations using latitude and longitude and find the shortest distance places from my location. Also I don't know how to get my current location from an android device using coding. I have heard that the location can be found out by two ways such as 1) using last known location and 2) by using onlocation changed. The current location using last known location method does not suite for my application. So I need to get my current latitude and longitude using onlocation changed. If its not available, then get the location coordinates from latitude and longitude. </p>
  
  <p>This is the code in which I have opened google maps in my app and marked a place using a latitude and longitude.</p>
  
  <pre><code>public class Map extends Activity {
  
  private GoogleMap googleMap;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_map);
      try {
          // Loading map
          initilizeMap();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
      double latitude =9.887262 ;
      double longitude = 76.731675;
  
      // create marker
      MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
  
      // adding marker
      googleMap.addMarker(marker);
      CameraPosition cameraPosition = new CameraPosition.Builder().target(
              new LatLng(latitude, longitude)).zoom(12).build();
  
  googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  
  
  }
  
  private void initilizeMap() {
      if (googleMap == null) {
          googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
  
          // check if map is created successfully or not
          if (googleMap == null) {
              Toast.makeText(getApplicationContext(),
                      "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                      .show();
          }
      }
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      initilizeMap();
  }
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.map, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  }
  </code></pre>
  
  <p>This is its layout file..</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" &gt;
  
  &lt;fragment
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.MapFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>