Displaying current user location in google maps only once - android

My app displays a map, and automatically takes user to their current location. There is a text box that allows the users to type their location to display a marker at the address typed on the text bar.
When I start the app, it correctly shows the user where they are located. When I type a new address on the map, it shows a marker at the newly typed location. However, after this it automatically takes the user back to where they are located and removes the marker from the user typed location.
I suppose the onLocationChnaged fires multiple times. Does anyone know what's the solution to find the user location just once when the app is started?
This is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (view != null)
{
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view); }
try
{
view = inflater.inflate(R.layout.fragment_map_view, container, false);
}
catch (InflateException e)
{ /* map is already there, just return view as it is */ }
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map_fragment)).getMap();
map.setMyLocationEnabled(true);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DateTimeFragment datetime=new DateTimeFragment();
ft.add(R.id.datetime_container_map, datetime);
//SupportMapFragment mapFragment=null;
//if(mapFragment==null)
// mapFragment= (SupportMapFragment)fm.findFragmentById(R.id.map_fragment);
ft.commit();
AutoCompleteTextView location= (AutoCompleteTextView)view.findViewById(R.id.location_map);
location.setAdapter(new PlacesAutoCompleteAdapter(getActivity(),R.layout.list_layout));
location.setOnItemClickListener(this);
Spinner category=(Spinner)view.findViewById(R.id.category_query_map);
ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(this.getActivity().getBaseContext(),R.array.category_names, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
category.setAdapter(adapterCategory);
category.setOnItemSelectedListener(this);
return view;
}
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
String address_text = (String) adapterView.getItemAtPosition(position);
Geocoder geocoder = new Geocoder(getActivity().getBaseContext());
List<Address> addresses=null;
LatLng latlng;
MarkerOptions markerOptions;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = geocoder.getFromLocationName(address_text, 3);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses==null || addresses.size()==0){
Toast.makeText(getActivity().getBaseContext(), "No Location found", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
map.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
latlng = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getCountryName());
markerOptions = new MarkerOptions();
markerOptions.position(latlng);
markerOptions.title(addressText);
map.addMarker(markerOptions);
// Locate the first location
if(i==0)
map.animateCamera(CameraUpdateFactory.newLatLng(latlng));
}
}
#Override
public void onLocationChanged(Location location) {
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("my position");
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
Does anyone know why after displaying the marker in the user typed address, the marker is automatically updated to the user location?
PS: I also wanted to know on a side note, if it is a good practice to convert string to address using Geocoder as shown..or should I be placing this inside an AsyncTask

Could it be that your onLocationChanged() is firing and updating the marker to the current location?
You can guard against multiple updates:
private Location mLocation = null;
#Override
public void onLocationChanged(Location location) {
if (mLocation == null) {
mLocation = location;
map.clear();
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(location.getLatitude(), location.getLongitude()));
mp.title("my position");
map.addMarker(mp);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
}
}

Related

onSearch method inside fragment

How can I turn this method to be used in a fragment class? I am unable to use this method in a fragment (onCreate).
public void onSearch(View view) {
EditText location_tf = (EditText) findViewById(R.id.TFaddress);
String location = location_tf.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
I do not know how the inclusion of an action through this method onSearch , the layout is ready to receive the method but do not know how to insert it in the class. I 'm trying to insert the onSearch method in this code :
public class FragmentC extends Fragment{
MapView mMapView;
private GoogleMap googleMap;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.frag4, container,
false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroyView();
// Log.d(TAG, "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
Assuming that mMapView is the same as mMap, and that theres a button that triggers the onSearch method, here's how your Fragment code should look like. Pay attention to the onCreateView changes and that the method onSearch does not recieves a View param:
private EditText mLocationEditText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.frag4, container,
false);
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
// Obtain the EditText view reference first
mLocationEditText = (EditText) v.findViewById(R.id.TFaddress);
// Obtain the button that triggers the onSearch reference
mButtonThatTriggersSearch = (Button) v..findViewById(R.id.search_button_id);
// Set the onClickListener that should excecute when someome clicks the button
mButtonThatTriggersSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
onSearch();
}
});
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 17.385044;
double longitude = 78.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
private void onSearch() {
String location = mLocationEditText.getText().toString();
List<Address> addressList = null;
if (location != null || !location.equals("")) {
Geocoder geocoder = new Geocoder(getActivity());
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mLocationEditText.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mLocationEditText.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}

Insert markers with different icon from my list drawable

I want to add marker on map with long click. but it is not working. normal click is working.
This is my code.
public class Map extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//show error dialog if GoolglePlayServices not available
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.map);
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
// Getting GoogleMap object from the fragment
googleMap = supportMapFragment.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
#Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
// Getting latitude of the current location
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng).title("you are here" ));
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(5));
// Setting latitude and longitude in the TextView tv_location
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
public void onMapClick (LatLng point) {
// Do Something
googleMap =
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();
googleMap.addMarker(new MarkerOptions()
.position(point)
.title("TouchPoint"));
}
public void onMapLongClick(LatLng point) {
googleMap.addMarker(new MarkerOptions().position(point).title(
point.toString()));
Toast.makeText(getApplicationContext(),
"New marker added#" + point.toString(), Toast.LENGTH_LONG)
.show();
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
How can i solve this ?
In onCreate
googleMap.setOnMapLongClickListener(longClickListener);
Inside the activity
private OnMapLongClickListener longClickListener = new OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng point) {
//do work }
}

Android google maps multiple markers are not displayed

I want to display multiply markers using values stored in database.But i can only see ma current location in map. May i know what is the mistake i have committed.
public class MainActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
JSONObject jsonobject;
JSONArray jsonarray;
Float Lati,Longi;
ProgressDialog mProgressDialog;
ArrayList<User> emp;
ArrayList<String> Latitude;
ArrayList<String> Longitude;
ArrayList<HashMap<String, Float>> oslist = new ArrayList<HashMap<String, Float>>();
HashMap<String, Float> map = new HashMap<String, Float>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if(status!=ConnectionResult.SUCCESS){
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
dialog.show();
}else { // Google Play Services are available
// Getting reference to the SupportMapFragment of activity_main.xml
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
new DownJSON().execute();
if(location!=null){
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
#Override
public void onLocationChanged(Location location) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// Setting latitude and longitude in the TextView tv_location
tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude );
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class DownJSON extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
float zoom=0;
emp = new ArrayList<User>();
Log.d("Inside DownJson","DownJson");
jsonobject = JSONParser.getJSONfromURLL("JSON URL");
try {
jsonarray = jsonobject.getJSONArray("User");
Log.d("Latitude",jsonarray.toString());
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
User e = new User();
Float Lati =Float.valueOf(jsonobject.optString("Latitude"));
Log.d("Latitude",jsonobject.optString("Latitude"));
Float Longi =Float.valueOf(jsonobject.optString("Longitude"));
Log.d("Latitude",jsonobject.optString("Longitude"));
map.put("lati", Lati);
map.put("longi", Longi);
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args)
{
for (String key : map.keySet()) {
Log.d("LatituInFor",String.valueOf(map.get("lati")));
Log.d("LongInFor",String.valueOf(map.get("longi")));
LatLng pinLocation = new LatLng(Float.valueOf(map.get("longi")),Float.valueOf(map.get("longi")));
Marker storeMarker = googleMap.addMarker(new MarkerOptions()
.position(pinLocation)
);
}
}
private void drawMarker(LatLng point){
// Creating an instance of MarkerOptions
MarkerOptions markerOptions = new MarkerOptions();
// Setting latitude and longitude for the marker
markerOptions.position(point);
// Adding marker on the Google Map
googleMap.addMarker(markerOptions);
}
}
}
May be here is problem
LatLng pinLocation = new LatLng(Float.valueOf(map.get("longi")),Float.valueOf(map.get("longi")));"
you have used "longi" in (map.get("longi") for both Latitute and Longitude

How to refresh google map apiv2

I have a timer.it is running every 5 minute.ı write a method called konumlarıAl in timer run method. this method get locations data from database.When konumlarıAl run,HaritaKonumGoster is method calling.I want to delete all markers and show new location data marker on map without refreshing page.
my code
private void HaritaKonumGoster() {
// TODO Auto-generated method stub
if (googleHarita == null) {
googleHarita = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.haritafragment))
.getMap();
if (googleHarita != null) {
googleHarita.clear();
if(mrks.size()!=0)
{
for (Marker marker: mrks) {
marker.remove();
}
mrks.clear();
}
googleHarita.setMyLocationEnabled(true);
LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
Criteria criteria=new Criteria();
String provider =locationManager.getBestProvider(criteria, true);
Location mylocation=locationManager.getLastKnownLocation(provider);
double latitude=0;
double longitude=0;
double mylatitude=0;
double myLongtitude=0;
//double latitude=enlem;
//double longitude=boylam;
if (mylocation != null){
mylatitude=mylocation.getLatitude();
myLongtitude=mylocation.getLongitude();
}
BitmapDescriptor bitmapDescriptor
= BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);
try{
for (int i = 0; i < degiskenler.taksici.size(); i++) {
latitude=Double.parseDouble(degiskenler.taksici.get(i).enlem.trim());
longitude=Double.parseDouble(degiskenler.taksici.get(i).boylam.trim());
LatLng istanbulKoordinat = new LatLng(latitude,longitude);
Marker m= googleHarita.addMarker(new MarkerOptions().position(istanbulKoordinat).title("Kız Kulesi").icon(bitmapDescriptor));
googleHarita.moveCamera(CameraUpdateFactory.newLatLngZoom(istanbulKoordinat, 7));
mrks.add(m);
// Toast.makeText(getBaseContext(),"harita :)",Toast.LENGTH_LONG).show();
}
}
catch(Exception exception) {
Toast.makeText(getBaseContext(),"harita olmadı",Toast.LENGTH_LONG).show();
}
googleHarita.addMarker(new MarkerOptions().position(new LatLng(mylatitude, myLongtitude)).title("you hereeee"));
}
}
}
location update every 5 minute method
private void LocationUpdateEvery5minute() {
// TODO Auto-generated method stub
zamanlayici = new Timer();
yardimci = new Handler(Looper.getMainLooper());
zamanlayici.scheduleAtFixedRate(new TimerTask()
{
#Override
public void run(){
yardimci.post(new Runnable()
{
public void run()
{ Toast.makeText(getBaseContext(), "timera girdi",Toast.LENGTH_SHORT).show();
konumlarıAl();
}
});
}
}, 0, ZAMAN);
}
Delete markers using
googleHarita.clear()
https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()
Just one call to this method should be enough to remove all the markers on your map.
Then, and add markers using
googleHarita.addMarker()
http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html
example here
I don't think you need to refresh any pages.
You have to use map.clear(); which will help you to removes all markers, polylines, polygons, overlays, etc from the map.

Convert Lat Long to address Google Maps ApiV2

I am making a demo app with Google Maps Api V2.
I have added a simple marker and I have made it draggable
Here is my code:
onCreate method()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_v2);
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setOnMapClickListener(this);
googleMap.setOnMarkerDragListener(this);
googleMap.setOnMapLongClickListener(this);
set info method()
googleMap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker arg0) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
LayoutInflater inflater = (LayoutInflater) MapsDemo.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_info, null);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(marker.getSnippet());
return v;
}
});
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setTiltGesturesEnabled(true);
addMarker method
marker = googleMap.addMarker(new MarkerOptions()
.position(ROMA)
.title("Hello")
.snippet("Nice Place")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.draggable(true));
#Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
#Override
public void onMarkerDragEnd(Marker marker) {
LatLng field = marker.getPosition();
System.out.println("LatitudenLongitude:" + field.latitude + " " + field.longitude);
}
#Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
}
#Override
public void onMapLongClick(LatLng latlng) {
// TODO Auto-generated method stub
}
#Override
public void onMapClick(LatLng latlng) {
// TODO Auto-generated method stub
}
}
Now I want the address to come out when the user clicks on the marker.
Simple question is : How to get the address( name) from Lat Long in API v2?
Try this:
public List<Address> getAddress() {
if (latitude != 0 && longitude != 0) {
try {
Geocoder geocoder = new Geocoder(context);
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Log.d("TAG", "address = " + address + ", city = " + city + ", country = " + country);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "latitude and longitude are null", Toast.LENGTH_LONG).show();
}
return addresses;
}
You should use the Geocoder available in the Android API. You will need to make a call to getFromLocation(latitude,longitude,maxResults) which will return a List<Address>.

Categories

Resources