[code]
public class MainActivity extends FragmentActivity {
private GoogleMap mGoogleMap;
Spinner mSprPlaceType;
String[] mPlaceType = null;
String[] mPlaceTypeName = null;
double mLatitude = 0;
double mLongitude = 0;
SupportMapFragment fragment;
Location loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlaceType = getResources().getStringArray(R.array.place_type);
mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);
mSprPlaceType = (Spinner) findViewById(R.id.spr_place_type);
ArrayList<Mekan> mekan = new ArrayList<Mekan>();
for (String i : mPlaceTypeName) {
mekan.add(new Mekan(i));
}
// <--Burada ayarlar ekranından gelen arama yarıçapıdeğeri alınır-->
Intent i = getIntent();
final int km = i.getIntExtra("newkm", 32);
Log.d("----", "-----" + km);
ArrayAdapter<Mekan> adapter = new ArrayAdapter<Mekan>(this,
android.R.layout.simple_spinner_item, mekan);
mSprPlaceType.setAdapter(adapter);
Button btnFind = (Button) findViewById(R.id.btn_find);
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
if (status != ConnectionResult.SUCCESS) { // Google Play Services are
// not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();
} else {
// Getting reference to the SupportMapFragment
fragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
// Getting Google Map
mGoogleMap = fragment.getMap();
// Enabling MyLocation in Google Map
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.moveCamera(CameraUpdateFactory.zoomTo(13));
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
mGoogleMap.getUiSettings().setMapToolbarEnabled(true);
konumal();
LatLng yeniKoordinat = new LatLng(mLatitude, mLongitude);
if (yeniKoordinat != null) {
mGoogleMap.moveCamera(CameraUpdateFactory
.newLatLng(yeniKoordinat));
// Toast.makeText(
// this,
// "" + yeniKoordinat.latitude + "\n"
// + yeniKoordinat.longitude, Toast.LENGTH_SHORT)
// .show();
}
}
public void konumal() {
gpsIzleyici yeniGpsIzleyici = new gpsIzleyici(MainActivity.this);
if (yeniGpsIzleyici.canGetLocation()) {
mLatitude = yeniGpsIzleyici.getLatitude();
mLongitude = yeniGpsIzleyici.getLongitude();
Log.d("-------------------" + mLongitude, "--------------------"
+ mLatitude);
if (mLatitude == 0.0 && mLongitude == 0.0) {
Toast.makeText(getApplicationContext(), "GPS KAPALI",
Toast.LENGTH_LONG).show();
yeniGpsIzleyici.showSettingsAlert();
} else {
LatLng yeniKoordinat = new LatLng(mLatitude, mLongitude);
mGoogleMap.moveCamera(CameraUpdateFactory
.newLatLng(yeniKoordinat));
Toast.makeText(
getApplicationContext(),
"Sizin konumunuz:\n\tLat: " + mLatitude + "\n\tLong: "
+ mLongitude, Toast.LENGTH_LONG).show();
}
} else {
// konum veris alınamazsa
// GPS ya da network açıkde değilse
// etkinleştimek için ayarlara giit
yeniGpsIzleyici.showSettingsAlert();
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
fragment.onDestroy();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
fragment.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
fragment.onPause();
}
@Override
public void onLowMemory() {
// TODO Auto-generated method stub
super.onLowMemory();
fragment.onLowMemory();
}
[/code]
[code]
package com.example.gezi;
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
public class gpsIzleyici extends Service implements LocationListener {
private final Context mContext;
// gps durumu için
boolean isGPSEnabled = false;
// network durumu için
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location loc1;
double lat;
double lng;
// güncelleme mesafesi
private static final long min_guncelleme_mesafesi = 10;
// min güncelleme süresi
private static final long min_guncelleme_ms = 1000 * 60 * 1;
protected LocationManager locationManager;
public gpsIzleyici(Context context) {
mContext = context;
locationAl();
}
public Location locationAl() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
min_guncelleme_ms, min_guncelleme_mesafesi, this);
Log.d("Network", "Network");
if (locationManager != null) {
loc1 = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (loc1 != null) {
lat = loc1.getLatitude();
lng = loc1.getLongitude();
}
}
}
if (isGPSEnabled) {
if (loc1 == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
min_guncelleme_ms, min_guncelleme_mesafesi,
this);
Log.d("GPS YETKİLİ", "GPS YETKİLİ ");
if (locationManager != null) {
loc1 = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc1 != null) {
lat = loc1.getLatitude();
lng = loc1.getLongitude();
}
}
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
return loc1;
}
public void gpsDurdur() {
if (locationManager != null) {
locationManager.removeUpdates(gpsIzleyici.this);
}
}
public double getLatitude() {
if (loc1 != null) {
lat = loc1.getLatitude();
}
return lat;
}
public double getLongitude() {
if (loc1 != null) {
lng = loc1.getLongitude();
}
return lng;
}
public boolean canGetLocation() {
return canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS AYARLAR");
// Setting Dialog Message
alertDialog
.setMessage("GPS açık değil.Gps'i açmak için ayarlar menüsüne gitmek ister misiniz?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
// lat = location.getLatitude();
// lng = location.getLongitude();
// LatLng latLng = new LatLng(lat, lng);
}
@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 IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
[/code]