Created
January 28, 2020 05:03
-
-
Save batuhankrbb/9f7a76818558fcfa380c1e88160eedf8 to your computer and use it in GitHub Desktop.
longclick hata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.batuhankarababa.bookapp; | |
import androidx.annotation.NonNull; | |
import androidx.core.app.ActivityCompat; | |
import androidx.core.content.ContextCompat; | |
import androidx.fragment.app.FragmentActivity; | |
import android.Manifest; | |
import android.content.Context; | |
import android.content.pm.PackageManager; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteStatement; | |
import android.location.Address; | |
import android.location.Geocoder; | |
import android.location.Location; | |
import android.location.LocationListener; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
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 java.io.IOException; | |
import java.sql.SQLData; | |
import java.util.List; | |
import java.util.Locale; | |
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener { | |
private GoogleMap mMap; | |
LocationManager locationManager; | |
LocationListener locationListener; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_maps); | |
// 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; | |
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); | |
locationListener = new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) { | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
}; | |
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ | |
ActivityCompat.requestPermissions(this,new String [] {Manifest.permission.ACCESS_FINE_LOCATION},1 ); | |
} | |
else { | |
mMap.clear(); | |
Location lastk = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
LatLng lastlocation = new LatLng(lastk.getLatitude(),lastk.getLongitude()); | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastlocation,13)); | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); | |
mMap.setOnMapLongClickListener(MapsActivity.this); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
if(grantResults.length>0){ | |
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){ | |
mMap.clear(); | |
Location lastk = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
LatLng lastlocation = new LatLng(lastk.getLatitude(),lastk.getLongitude()); | |
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastlocation,13)); | |
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener); | |
mMap.setOnMapLongClickListener(MapsActivity.this); | |
} | |
} | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
} | |
@Override | |
public void onMapLongClick(LatLng latLng) { | |
Toast.makeText(getApplicationContext(),"çalıştır",Toast.LENGTH_LONG).show(); | |
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); | |
String fulladdress = ""; | |
try { | |
List<Address> myadress =geocoder.getFromLocation(latLng.latitude,latLng.longitude,1); | |
if(myadress.size()>0){ | |
if(myadress.get(0).getCountryName() != null){ | |
fulladdress = fulladdress + myadress.get(0).getCountryName(); | |
} | |
} | |
if(myadress.size()>0){ | |
if(myadress.get(0).getThoroughfare() != null){ | |
fulladdress = fulladdress + myadress.get(0).getThoroughfare(); | |
} | |
} | |
if(myadress.size()>0){ | |
if(myadress.get(0).getCountryCode() != null){ | |
fulladdress = fulladdress + myadress.get(0).getCountryCode(); | |
} | |
} | |
mMap.addMarker(new MarkerOptions().title(fulladdress).position(latLng)); | |
Double latdouble = latLng.latitude; | |
Double longdouble = latLng.longitude; | |
String latstring = latdouble.toString(); | |
String longstring = longdouble.toString(); | |
SQLiteDatabase database = this.openOrCreateDatabase("Maps",MODE_PRIVATE,null); | |
database.execSQL("CREATE TABLE IF NOT EXISTS mapstable(id INTEGER PRIMARY KEY,myfulladress VARCHAR,latitude VARCHAR,longitude VARCHAR)"); | |
String sqlString; | |
sqlString = "INSERT INTO mapstable(myfulladress,latitude,longitude) VALUES (?,?,?)"; | |
SQLiteStatement statement = database.compileStatement(sqlString); | |
statement.bindString(1,fulladdress); | |
statement.bindString(2,latstring); | |
statement.bindString(3,longstring); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment