1. Are you ready for the Galaxy S20? Here is everything we know so far!

get my current location in a variable and to create a marker on Google map activity using json data

Discussion in 'Android Development' started by sonu gadewar, Aug 1, 2017.

  1. sonu gadewar

    sonu gadewar Lurker
    Thread Starter

    I am developing an android app .In this app i want to get my location (latitude and longitude) and want to show nearby markers under certain radius. And my Questions are :-

    Q1)How to retrieve or get my current location and the store it in variables (in terms of latitude and longitude) ? Suppose my co-ordinates are "2.3331 , 5.2223" , so how to get this in 2 variables ( lat and long).

    Q2)How to create a marker on Google map activity using json values (json data is coming from remote database) ? I am having the data( latitude and longitude ) in json of 3 locations , now from that data how to create markers on google map ?
     



    1. Download the Forums for Android™ app!


      Download

       
  2. IArdel

    IArdel Lurker

    Sonu,
    below you can find the code you need:

    Code (Text):
    1.  
    2. public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    3.  
    4.     //need this to get permission from user
    5.     public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    6.     public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    7.     private static final int PERMISSION_REQUEST_CODE = 1234;
    8.  
    9.     private LocationManager locationManager;
    10.     LatLng myGPSPosition, myNetworkPosition;
    11.  
    12.     [USER=1021285]@override[/USER]
    13.     protected void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.activity_main);
    16.  
    17.         locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    18.  
    19.         FragmentManager fragmentManager = this.getSupportFragmentManager();
    20.         SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
    21.                 .findFragmentById(map);
    22.  
    23.         supportMapFragment.getMapAsync(this);
    24.     }
    25.  
    26.     [USER=1021285]@override[/USER]
    27.     public void onMapReady(GoogleMap map) {
    28.         if (ActivityCompat.checkSelfPermission(getApplicationContext(), ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
    29.                 && ActivityCompat.checkSelfPermission(getApplicationContext(), ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    30.             requestPermissions();
    31.         } else {
    32.             map.setMyLocationEnabled(true);
    33.         }
    34.  
    35.         try {
    36.             Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    37.             //here what you need:
    38.             double latitude = locationGPS.getLatitude();
    39.             double longitude = locationGPS.getLongitude();
    40.             //create marker
    41.             myGPSPosition = new LatLng(latitude, longitude);
    42.             map.moveCamera(CameraUpdateFactory.newLatLngZoom(myGPSPosition, 15));
    43.             //add marker
    44.             map.addMarker(new MarkerOptions()
    45.                     .title("My GPS position")
    46.                     .position(myGPSPosition));
    47.         }catch (NullPointerException | IllegalArgumentException e){
    48.             e.printStackTrace();
    49.             Toast.makeText(this,"Unable to get GPS data",Toast.LENGTH_SHORT).show();
    50.         }
    51.  
    52.         try {
    53.             //the same, but using network proveder
    54.         Location locationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    55.             double latitude = locationNetwork.getLatitude();
    56.             double longitude = locationNetwork.getLongitude();
    57.         myNetworkPosition = new LatLng(latitude, longitude);
    58.             map.moveCamera(CameraUpdateFactory.newLatLngZoom(myNetworkPosition, 15));
    59.             map.addMarker(new MarkerOptions()
    60.                     .title("My Network position")
    61.                     .position(myNetworkPosition));
    62.         }catch (NullPointerException | IllegalArgumentException e){
    63.             e.printStackTrace();
    64.             Toast.makeText(this,"Unable to get Network data",Toast.LENGTH_SHORT).show();
    65.         }
    66.  
    67.     }
    68.  
    69.     public void requestPermissions() {
    70.         ActivityCompat.requestPermissions(this,
    71.                 new String[]{
    72.                         ACCESS_COARSE_LOCATION,
    73.                         ACCESS_FINE_LOCATION
    74.                 },
    75.                 PERMISSION_REQUEST_CODE);
    76.     }
    77. }
    78.  
     
    #2 IArdel, Aug 2, 2017
    Last edited by a moderator: Aug 2, 2017
    sonu gadewar and Deleted User like this.
  3. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    I moved this to the development area for better exposure.
     
    sonu gadewar and Deleted User like this.
  4. sonu gadewar

    sonu gadewar Lurker
    Thread Starter

    thank you :)
     
    IArdel likes this.
  5. sonu gadewar

    sonu gadewar Lurker
    Thread Starter

    thank you :)
     
    Unforgiven likes this.
  6. There is a complete tutorial? I want to make a full project, where i can put a marker on the map when the accelerometer get the value i want.
     
    #6 Nedson Donato Soares, Oct 3, 2017
    Last edited: Oct 4, 2017
Loading...

Share This Page

Loading...