1. Download our Official Android App: Forums for Android!

Apps How to get Latitude and Longitude?

Discussion in 'Android Development' started by alhazza944, Mar 9, 2016.

  1. alhazza944

    alhazza944 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Mar 7, 2016

    Mar 7, 2016
    2
    0
    5
    Male
    Hi all,
    I am creating android app ( google map)
    the target of app is when it lunched, the marker will be in specific location ( i did this)
    I added auto complete fragment for the searching

    However, I do not know how to get the destination Latitude and Longitude to put in Geocoder . I tried many things but it did not work


    Code (Java):
    1. package com.example.mohammed.midterm;
    2.  
    3. import android.Manifest;
    4. import android.app.Activity;
    5. import android.content.Context;
    6. import android.content.Intent;
    7. import android.location.Address;
    8. import android.location.Geocoder;
    9. import android.location.Location;
    10. import android.location.LocationManager;
    11. import android.support.v4.app.FragmentActivity;
    12. import android.os.Bundle;
    13. import android.support.v4.content.ContextCompat;
    14. import android.util.Log;
    15. import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
    16. import com.google.android.gms.common.GooglePlayServicesRepairableException;
    17. import com.google.android.gms.common.GooglePlayServicesUtil;
    18. import com.google.android.gms.common.api.Status;
    19. import com.google.android.gms.location.places.Place;
    20. import com.google.android.gms.location.places.PlaceBuffer;
    21. import com.google.android.gms.location.places.Places;
    22. import com.google.android.gms.location.places.ui.PlaceAutocomplete;
    23. import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
    24. import com.google.android.gms.location.places.ui.PlaceSelectionListener;
    25. import com.google.android.gms.maps.CameraUpdateFactory;
    26. import com.google.android.gms.maps.GoogleMap;
    27. import com.google.android.gms.maps.OnMapReadyCallback;
    28. import com.google.android.gms.maps.SupportMapFragment;
    29. import com.google.android.gms.maps.model.LatLng;
    30. import com.google.android.gms.maps.model.LatLngBounds;
    31. import com.google.android.gms.maps.model.Marker;
    32. import com.google.android.gms.maps.model.MarkerOptions;
    33.  
    34. import java.io.IOException;
    35. import java.util.List;
    36.  
    37. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    38.  
    39.     private GoogleMap mMap;
    40.     public static final String TAG = Activity.class.getSimpleName();
    41.     @Override
    42.     protected void onCreate(Bundle savedInstanceState) {
    43.         // Assume thisActivity is the current activity
    44.         int permissionCheck = ContextCompat.checkSelfPermission(this,
    45.                 Manifest.permission.ACCESS_COARSE_LOCATION);
    46.         int permissionCheck1 = ContextCompat.checkSelfPermission(this,
    47.                 Manifest.permission.WRITE_EXTERNAL_STORAGE);
    48.         int permissionCheck2 = ContextCompat.checkSelfPermission(this,
    49.                 Manifest.permission.ACCESS_FINE_LOCATION);
    50.         int permissionCheck3 = ContextCompat.checkSelfPermission(this,
    51.                 Manifest.permission.READ_EXTERNAL_STORAGE);
    52.         int permissionCheck4 = ContextCompat.checkSelfPermission(this,
    53.                 Manifest.permission.INTERNET);
    54.         super.onCreate(savedInstanceState);
    55.         setContentView(R.layout.activity_maps);
    56.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    57.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
    58.                 .findFragmentById(R.id.map);
    59.         mapFragment.getMapAsync(this);
    60.  
    61.         PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
    62.                 getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
    63.  
    64.                 String location = autocompleteFragment.toString();
    65.         List<Address> addressList = null;
    66.         Geocoder geocoder = new Geocoder(this);
    67.  
    68.         try {
    69.             addressList = geocoder.getFromLocationName(location,1);
    70.         }catch (IOException e){
    71.             e.printStackTrace();
    72.         }
    73.  
    74.         Address address = addressList.get(0);
    75.         LatLng latLng = new LatLng(address.getLatitude(),address.getLongitude());
    76.         mMap.addMarker(new MarkerOptions().position(latLng).title("Destination"));
    77.         mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    78.         autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
    79.             @Override
    80.             public void onPlaceSelected(Place place) {
    81.                 // TODO: Get info about the selected place.
    82.                Log.i(TAG, "Place: " + place.getName());
    83.             }
    84.             @Override
    85.             public void onError(Status status) {
    86.                 // TODO: Handle the error.
    87.                 Log.i(TAG, "An error occurred: " + status);
    88.             }
    89.         });
    90.    }
    91.      */
    92.     @Override
    93.     public void onMapReady(GoogleMap googleMap) {
    94.         mMap = googleMap;
    95.  
    96.         // Add a marker in SJU and move the camera
    97.         LatLng sju = new LatLng(39.995251, -75.238896);
    98.         mMap.addMarker(new MarkerOptions().position(sju).title("Saint Joseph's University"));
    99.         mMap.moveCamera(CameraUpdateFactory.newLatLng(sju));
    100.         Marker obj = mMap.addMarker(new MarkerOptions()
    101.                 .position(sju)
    102.                 .title("SJU")
    103.                 .snippet("Nice School"));
    104.         obj.showInfoWindow();
    105.         mMap.moveCamera(CameraUpdateFactory.zoomTo(15));
    106.  
    107.     }
    108. }
    109.  
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,872
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,872
    11,505
    1,988
    Male
    Software developer
    South West of England

Share This Page

Loading...