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

Google Play Service out of date error
Display video stored in SD card in a GridView
Android Studio - Is it mandatory to convert existing project layouts to constraint layout?
Reliable way to prompt mobile user for image gallery apps from web page
How to show OFF state of switch from Code
android.view.InflateException: Binary XML file : Error inflating class fragment
Robot Framework: Invalid argument specification: Only last argument can be kwargs
Tabhost content not showing when i upgrade android.support.v4
Offline G-Voice Recognition of Certain Words
Volley & AsyncTask : Response orrder
Android SQLite: Replace old database with a new one or use migration scripts
React-Native + Android, only build for armeabi architecture
ListView tools:listitem not working in android studio 2.2 stable channel
Unable to create emulator with Target Android 4.1(Google APIs) in Studio 2.2
Hybrid app crashing in Android studio with error: Session 'android': Error Launching activity
Android Studio- ViewHolder don't change fields of view; Array adapter is not used;

Categories

HOME
eclipse
cobalt
delphi-7
prism
rdbms
r-googlesheets
static-site
dojo
emscripten
google-admin-sdk
google-cloud-datalab
crystal-lang
jxl
ipmitool
http-post
smartclient
windows-xp
kendo-mobile
zxing
beta
xunit
game-theory
pic32
shared
iis-express
jaws
postback
cart
fstream
dynamic-jasper
html-encode
slacktextviewcontroller
lubridate
salesforce-communities
wurfl
tex
odroid
kill
odoo-website
dbnull
movilizer
subclipse
lpsolve
fwrite
proget
term
hammer.js
mongotemplate
arq
scalding
tinkerpop
appdomain
janus
gets
angular2-testing
hadoop-2.7.2
scaffolding
expectations
http-status-code-401
gcloud-ruby
highland.js
file-move
django-urls
ons-api
text-search
teamcity-9.1
actionfilterattribute
mmwormhole
contrast
grails-domain-class
ios8-extension
vbe
jammer
ebay-lms
rolify
route-provider
kaleidoscope
midlet
zope.interface
saleslogix
fusefabric
azman
node-redis
flex-mobile
catransform3d
umra
metaweblog
undefined-index
google-wave
int64
information-management
change-management

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