package
com.ProjectMyPosition;
import
java.util.List;
import
android.app.AlertDialog;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.Intent;
import
android.graphics.drawable.Drawable;
import
android.location.Criteria;
import
android.location.Location;
import
android.location.LocationListener;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.util.Log;
import
com.google.android.maps.GeoPoint;
import
com.google.android.maps.MapActivity;
import
com.google.android.maps.MapController;
import
com.google.android.maps.MapView;
import
com.google.android.maps.Overlay;
import
com.google.android.maps.OverlayItem;
public
class
ProjectMyPosition
extends
MapActivity {
private
static
final
String TAG = ProjectMyPosition.
class
.toString();
private
static
boolean
result =
false
;
private
LocationManager locationManager;
private
String provider;
private
Location location;
private
MapView mapView;
private
MapController mapController;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState){
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(
true
);
loadGps();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable =
this
.getResources().getDrawable(R.drawable.androidmarker);
MyItemizedOverlay itemizedoverlay =
new
MyItemizedOverlay(drawable,
this
);
GeoPoint point =
new
GeoPoint((
int
)(location.getLatitude()*1E6),(
int
)(location.getLongitude()*1E6));
OverlayItem overlayitem =
new
OverlayItem(point,
"Hi Pondol!"
,
"I'm in Seoul!"
);
itemizedoverlay.addOverlay(overlayitem);
mapController = mapView.getController();
mapController.setCenter(point);
mapController.setZoom(
15
);
mapOverlays.add(itemizedoverlay);
}
@Override
protected
boolean
isRouteDisplayed() {
return
false
;
}
public
void
loadGps() {
Log.w(TAG ,
"loadGps Start"
);
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria =
new
Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(
false
);
criteria.setBearingRequired(
false
);
criteria.setSpeedRequired(
false
);
criteria.setCostAllowed(
true
);
provider = locationManager.getBestProvider(criteria,
true
);
Log.i(TAG,
"provider:"
+provider);
if
(provider ==
null
){
result = chkGpsService();
if
(result){
loadGps();
}
}
else
{
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider,
1000
,
5
, loclistener);
if
(location ==
null
){
location = locationManager.getLastKnownLocation(provider);
if
(location ==
null
){
Log.w(TAG,
"get Location From GPS Fail !!!!!"
);
AlertDialog.Builder adb =
new
AlertDialog.Builder(ProjectMyPosition.
this
);
adb.setPositiveButton(
"OK"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
}
});
adb.setTitle( R.string.Location_fail_title );
adb.setMessage(R.string.Location_fail_message);
adb.show();
}
}
}
}
private
final
LocationListener loclistener =
new
LocationListener(){
public
void
onLocationChanged(Location location) {
Log.w(TAG ,
"onLocationChanged"
);
}
public
void
onProviderDisabled(String provider) {
Log.w(TAG ,
"onProviderDisabled"
);
}
public
void
onProviderEnabled(String provider) {
Log.w(TAG ,
"onProviderEnabled"
);
}
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {
Log.w(TAG ,
"onStatusChanged"
);
}
};
private
boolean
chkGpsService() {
String gs = android.provider.Settings.Secure.getString(getContentResolver(),
android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.w(
"chkGpsService"
,
"get GPs Service"
);
if
(gs.indexOf(
"gps"
,
0
) <
0
) {
Log.w(
"chkGpsService"
,
"status: off"
);
AlertDialog.Builder gsDialog =
new
AlertDialog.Builder(
this
);
gsDialog.setTitle(
"GPS Status OFF !!!"
);
gsDialog.setMessage(
"Change Setting !!"
);
gsDialog.setPositiveButton(
"OK"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
Intent intent =
new
Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}).create().show();
return
false
;
}
else
{
Log.w(
"chkGpsService"
,
"status: on"
);
return
true
;
}
}
}