-1

How Exactly The Google Map give the location means what is the Technic google Map calculate the latitude and longitude and show the exact location on Map according to these latitude and longitude (Its not specific for Android I am talking in general).

But in Context of Android There are three way for getting the location (Latitude and Longitude)

  • First one is Location Manager its in core of Android SDK.
  • Second one is Location Client comes in google play services library which is now deprecated.
  • Third one is Google Api Client which also comes in google play services library.

So in Context of android for getting the current location there is also one option in LocationApiClient And GoogleApiClient that we can get the location on the basis of Accuracy with the help of LocationRequest class which is in google play services library, and Location Request class has different flag for getting the location on basis of accuracy so if i will give the best location then it will give the accurate location and if i will give the low accuracy flag then it will give the location with long distance or accuracy may be around one Km or two Km.

So I assume that if I will give the best accuracy flag then it will go to the GPS (Satellite) to get the location and for low accuracy flag it will go to the cell tower or may be from WiFi.

"so here my question is that if it will not found location from satellite then will it go to the cell tower to get the location in best accuracy flag."

Please Reply For My Questions I Want To Know In Deep.

Ashish Kakkad
  • 23,020
  • 11
  • 96
  • 132
  • As per my knowledge its work on Haversine algorithm. and you can refer this link http://stackoverflow.com/questions/22577075/calculating-the-distance-between-two-latitude-and-longitude-points-in-android – Ajay Pandya May 09 '15 at 07:31
  • Hello Ajay I want to know that how the google get my location and if by lat and long then how it get lat and long and calculate location according these lat and long. – Virendra Pal Singh May 09 '15 at 07:41
  • i think for that you have to need to get help from GOOGLE not from stack :P lol :) – Ajay Pandya May 09 '15 at 07:44

1 Answers1

0
btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(LocationUserActivity.this,
                    DriverListActivity.class);
            intent.putExtra("userlat", lat);
            intent.putExtra("userlon", lon);
            Bundle bndlanimationn = ActivityOptions.makeCustomAnimation(
                    getApplicationContext(), R.anim.animation_one,
                    R.anim.animation_two).toBundle();
            startActivity(intent, bndlanimationn);

        }
    });
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
    getLocation();
    /*
     * LocationManager locationManager = (LocationManager)
     * getSystemService(LOCATION_SERVICE);
     * locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
     * 0, 0, this);
     * 
     * Location location = locationManager
     * .getLastKnownLocation(LocationManager.GPS_PROVIDER);// lat =
     * location.getLatitude(); lon = location.getLongitude();
     */
    LatLng loc = new LatLng(lat, lon);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 15));
    map.addMarker(new MarkerOptions().position(loc).title("Your Location"));
    arrayList = new ArrayList<NameValuePair>();
    arrayList.add(new BasicNameValuePair("whichfunction", "getalllocs"));
    //new AllCallAsynch().execute();

}

public Location getLocation() {
    try {
        locationManager = (LocationManager) this
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        lat = location.getLatitude();
                        lon = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            lat = location.getLatitude();
                            lon = location.getLongitude();
                        }
                    }
                }
            }
        }

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

    return location;
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    lat = location.getLatitude();
    lon = location.getLongitude();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

private class AllCallAsynch extends AsyncTask<Void, Void, Void> {
    ProgressDialog dialog;
    ArrayList<LatLng> points = new ArrayList<LatLng>();

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        dialog = new ProgressDialog(LocationUserActivity.this);
        dialog.setMessage("Loading...");
        dialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {

        // TODO Auto-generated method stub
        JSONParser parser = new JSONParser();
        JSONObject jsonObject = parser.getJSONFromUrl(url, arrayList);
        try {
            jsonArray = jsonObject.getJSONArray("locs");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                String id = object.getString("id");
                String lat = object.getString("latitude");
                String lon = object.getString("longitude");
                String mail = object.getString("email");
                points.add(new LatLng(Double.parseDouble(lat), Double
                        .parseDouble(lon)));

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        for (int i = 0; i < points.size(); i++) {
            LatLng ln = points.get(i);
            map.addMarker(new MarkerOptions()
                    .position(ln)
                    .title("Driver Location")
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
        }
        /*
         * for (LatLng point : points) { // plot the point to the map
         * map.addMarker(new MarkerOptions() .position(point)
         * .title("Driver Location") .icon(BitmapDescriptorFactory
         * .defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); }
         */
        dialog.dismiss();
    }
}

}