android


How to find the nearest location using android studio and json?


I will add the Button "nearest", so if I click the button "nearest", maps will show 3 nearest location from user. anyone can help me? how to find the nearest location? i'm using android studio and database from mysql using json volley. i try to find a tutorial about this, but not found, if you have or find a tutorial about this, please give me a link. i'm newbie.
This is my coding Maps.java
private void initMap() {
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
}
public boolean googleServicesAvailable() {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isAvailable = api.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (api.isUserResolvableError(isAvailable)) {
Dialog dialog = api.getErrorDialog(this, isAvailable, 0);
dialog.show();
} else {
Toast.makeText(this, "can't connect to play services", Toast.LENGTH_LONG).show();
}
return false;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
getLokasi();
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
mMap.getUiSettings().setMapToolbarEnabled(true);
}
private void getLokasi() {
String url = "http://sempatbimbel.esy.es/actions/jsontbimbel.php";
JsonArrayRequest request = new JsonArrayRequest
(Request.Method.GET, url, new Response.Listener<JSONArray>() {
public void onResponse(JSONArray response) {
numData = response.length();
Log.d("DEBUG", "PARSE JSON");
latLng = new LatLng[numData];
markerD = new Boolean[numData];
id_bimbel = new int [numData];
foto = new String[numData];
namabimbel = new String[numData];
Alamat = new String[numData];
kecamatan = new String[numData];
latitude = new Double[numData];
longitude = new Double[numData];
kontak = new String[numData];
biaya = new String[numData];
for (int i = 0; i < numData; i++) {
try {
JSONObject data = response.getJSONObject(i);
id_bimbel[i] = data.getInt("id_bimbel");
latLng[i] = new LatLng(data.getDouble("latitude"), data.getDouble("longitude"));
foto[i] = data.getString("foto");
namabimbel[i] = data.getString("namabimbel");
Alamat[i] = data.getString("Alamat");
kecamatan[i] = data.getString("kecamatan");
latitude[i] = data.getDouble("latitude");
longitude[i] = data.getDouble("longitude");
kontak[i] = data.getString("kontak");
biaya[i] = data.getString("biaya");
markerD[i] = false;
mMap.addMarker(new MarkerOptions()
.position(latLng[i])
.title(namabimbel[i])
.snippet(Alamat[i])
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
} catch (JSONException e) {
e.printStackTrace();
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng[i], 12f));
}
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Log.d("DEBUG", "Marker Clicked");
for (int i=0; i<numData; i++){
if (marker.getTitle().equals(namabimbel[i])){
if (markerD[i]){
Log.d("DEBUG", "panggil activity");
openDetailActivity(id_bimbel[i],foto[i],namabimbel[i],Alamat[i],kontak[i],biaya[i],kecamatan[i],latitude[i],longitude[i]);
markerD[i]=false;
} else {
Log.d("DEBUG", "show info");
markerD[i] = true;
marker.showInfoWindow();
Toast toast = Toast.makeText(getApplicationContext(), "Tekan sekali lagi untuk melihat detail", Toast.LENGTH_LONG); toast.show();
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (tv != null)
tv.setGravity(Gravity.CENTER);
toast.show();
}
} else {
markerD[i] = false;
}
}
return false;
}
});
}
} , new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error){
AlertDialog.Builder builder = new AlertDialog.Builder(Maps.this);
builder.setTitle("Error!");
builder.setMessage("No Internet Connection");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
getLokasi();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
Volley.newRequestQueue(this).add(request);
}
private void openDetailActivity (Integer id_bimbel, String foto,String namabimbel, String Alamat, String kontak, String biaya, String kecamatan, Double latitude, Double longitude )
{
Intent i=new Intent(Maps.this, Detail.class);
//Data
i.putExtra("id_bimbel", id_bimbel);
i.putExtra("foto",foto);
i.putExtra("namabimbel",namabimbel);
i.putExtra("Alamat",Alamat);
i.putExtra("kontak",kontak);
i.putExtra("biaya",biaya);
i.putExtra("kecamatan",kecamatan);
i.putExtra("latitude",latitude);
i.putExtra("longitude",longitude);
Maps.this.startActivity(i);
}
}

Related Links

A different make file for each architecture - Android NDK
onListItemClick stops working after one click
non full screen activity
Referencing the Facebook SDK in my Android project
How to add Android-ViewPagerIndicator library to maven project
Where are Shared Preferences saved on Nexus S? [duplicate]
Android Spawn Enemies by time
Using CursorLoader to get contacts causes duplication
Make Datepicker look on Android 4.0 as 2.x
getting error as document is empty(size 0kb) when opening pdf file in micromax funbook
Accept a RatingBar value when the default value is selected
How to correctly sign apps in CyanogenMod?
Getting GIDs from any Android Application
Runtime Crash on constructing Gson object with NoClassDefFoundError
HTML5 Geolocation not working in Android Emulator
Getting error while generating the .apk file if proguard is ON

Categories

HOME
sonarqube
design-patterns
checkstyle
fuelux
gdb
eslint
codenvy
twig
character-encoding
case
ms-access-2016
soa
switch-statement
zip
python-multithreading
jtextpane
flume
barcode
joomla3.0
bing-maps
jquery-ui-sortable
google-chrome-app
gson
activecollab
google-form
stellar.js
automatic-ref-counting
onedrive-api
scrolltop
internationalization
mvc-mini-profiler
replication
ag
jquery-ias
r-mice
jboss6.x
ckfinder
froala
ntp
anki
spring-security4
android-gridview
hard-drive
xajax
protege4
reactivekit
modernizr
odroid
railstutorial.org
resolution
glm-math
berkeley-db-je
jide
httrack
term
cnc
posixct
sundials
janus
trigger.io
smartfoxserver
lrs
tooleap
php-socket
aurora
false-sharing
jaunt-api
linklabel
toran-proxy
dynatree
file-move
video-player
fuelcms
text-search
amiga
linked-tables
pyobjc
botan
omnifaces
zxspectrum
variadic-templates
dbaccess
rickshaw
encryption-asymmetric
cuba
live555
draw2d-js
xmltodict
windows-embedded-standard
solace-mq
mongohq
composite-component
server-administration
swfobject
sqlclr
onmousemove
apache-whirr
cgbitmapcontextcreate
timthumb
inline-formset
openfaces
web-application-project
scrubyt
entitykey
image-capture
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App