Manifest
<application
android:allowBackup="true"
android:icon="@drawable/ic_shoes"
android:label="SOJI Cuci Sepatu"
android:roundIcon="@drawable/ic_shoes"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.apps_jasasepatu">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBQw3ZexsBHWcRf4iHCBGE18rizoy_G9I8" />
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
Gradle:
android {
compileSdk 31
dataBinding{
enabled=true
}
buildFeatures {
viewBinding true
}
defaultConfig {
...
implementation 'com.google.android.gms:play-services-maps:18.0.2'
activity_map.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />MAP.javapackage com.example.apps_jasasepatu;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.example.apps_jasasepatu.databinding.ActivityMapsBinding;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private ActivityMapsBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}permission:<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>ATAU U GPS DAN DATABASE DENGAN CODE SBBpackage com.example.apps_jasasepatu;
import androidx.fragment.app.FragmentActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.example.apps_jasasepatu.databinding.ActivityMapsBinding;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
int jd=0;
String[]arKode;
double[]arJarak;
double[]arLat;
double[]arLon;
String[]arNama;
String[]arKet;
String myLati = "-6.18534";
String myLongi = "106.77894";
String myPosisi = "Posisi GPS Anda";
double slat=0;
double slon=0;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONArray myJSON = null;
ArrayList<HashMap<String, String>> arrayList;
private static final String TAG_SUKSES = "sukses";
private static final String TAG_record = "record";
private static final String TAG_id_cabang = "id_cabang";
private static final String TAG_atas = "atas";
private static final String TAG_bawah = "bawah";
private GoogleMap mMap;
private ActivityMapsBinding binding;
String ip="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_maps);
binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
arrayList = new ArrayList<HashMap<String, String>>();
ip=jParser.getIP();
Intent i = getIntent();
myLati=i.getStringExtra("myLati");
myLongi=i.getStringExtra("myLongi");
myPosisi=i.getStringExtra("myPosisi");
try{
arrayList.clear();
}
catch(Exception ee){}
new load().execute();
}
class load extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(ip+"cabang/cabang_show.php", "GET", params);
Log.d("show: ", json.toString());
try {
int sukses = json.getInt(TAG_SUKSES);
if (sukses == 1) {
myJSON = json.getJSONArray(TAG_record);
jd=myJSON.length();
arNama=new String[jd];
arKet=new String[jd];
arKode=new String[jd];
arJarak=new double[jd];
arLat=new double[jd];
arLon=new double[jd];
try{
slat=Double.parseDouble(myLati);
slon=Double.parseDouble(myLongi);
}
catch(Exception ww){
}
for (int i = 0; i < jd; i++) {
JSONObject c = myJSON.getJSONObject(i);
String id_cabang = c.getString(TAG_id_cabang);
String atas = c.getString(TAG_atas);
String bawah = c.getString(TAG_bawah);
String lat = c.getString("latitude");
String lon = c.getString("longitude");
double dlat=0;
double dlon=0;
double jarak=0;
try{
dlat=Double.parseDouble(lat);
dlon=Double.parseDouble(lon);
jarak=jParser.getJarak(slat,slon,dlat,dlon);
}
catch(Exception ww){
}
arLat[i]=dlat;
arLon[i]=dlon;
arKode[i]=id_cabang;
arNama[i]=atas;
arKet[i]=bawah;
arJarak[i]=jarak;
}
}
}
catch (JSONException e) {e.printStackTrace();}
return null;
}
protected void onPostExecute(String file_url) {
runOnUiThread(new Runnable() {
public void run() {
myMap();
}
});}
}
void myMap(){
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng Pos = new LatLng(slat, slon);
mMap.addMarker(new MarkerOptions().position(Pos).title("Posisi Anda "+myPosisi));
mMap.addMarker(new MarkerOptions().position(Pos).title("Posisi Anda "+myPosisi).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
//mMap.moveCamera(CameraUpdateFactory.newLatLng(Pos));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Pos, 13));
for (int i = 0; i < jd; i++) {
Pos = new LatLng(arLat[i],arLon[i]);
mMap.addMarker(new MarkerOptions().position(Pos).title(arNama[i]).snippet(arKet[i]).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(Pos));
}
}
}
// BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.pin);
// LatLng latLng = new LatLng(21.4225, 39.8262);
// googleMap.addMarker(new MarkerOptions().position(latLng).title("Mecca").icon(icon)).showInfoWindow();// googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
Tidak ada komentar:
Posting Komentar