public class MapActivity extends FragmentActivity implements OnClickListener, LocationListener {
private GoogleMap map;
private Button satelite, hybrid, standard;
private ImageView img_back;
private LocationManager locationManager;
double currentLatitude, currentLongitude, lat_loc, lon_loc;
private String imeNumber;
private Geocoder geocoder;
private List<Address> currentAddresses;
private TextView title, subTitle;
String currentDateString;
String logEntryJsonString;
String userMobileNo;
String contactUserName;
String module = "ZappIt";
String user_mesg = "";
public final String PROPERTY_REG_ID = "registration_id";
public static Boolean zappStatus = false;
public static Boolean requestStatus = false;
public static String msg;
public EditText edit_userMesg;
String flag;
private static final long MIN_TIME = 5000;
private static final float MIN_DISTANCE = 1;
Circle circle;
Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
imeNumber = mngr.getDeviceId();
initViewController();
// Getting Google Play availability status
int status = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(getBaseContext());
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are
// not available
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this,
requestCode);
dialog.show();
} else {
geocoder = new Geocoder(getApplicationContext(),
Locale.getDefault());
// Getting reference to the SupportMapFragment of activity_main.xml
// Getting GoogleMap object from the fragment
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// Enabling MyLocation Layer of Google Map
map.setMyLocationEnabled(true);
// Getting LocationManager object from System Service
// LOCATION_SERVICE
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(provider, MIN_TIME,
MIN_DISTANCE, this);
}
}
@Override
protected void onResume() {
super.onResume();
}
private void initViewController() {
satelite = (Button) findViewById(R.id.button_satelite);
hybrid = (Button) findViewById(R.id.button_hybrid);
standard = (Button) findViewById(R.id.button_standerd);
img_back = (ImageView) findViewById(R.id.map_img_back);
title = (TextView) findViewById(R.id.txt_map_title);
subTitle = (TextView) findViewById(R.id.txt_map_sub_title);
edit_userMesg = (EditText) findViewById(R.id.zapp_msg);
img_back.setOnClickListener(this);
standard.setOnClickListener(this);
hybrid.setOnClickListener(this);
satelite.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_satelite:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.button_hybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
case R.id.button_standerd:
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.map_img_back:
this.finish();
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
return true;
}
return false;
}
@Override
public void onLocationChanged(Location location) {
map.clear();
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
double lat = location.getLatitude();
double lng = location.getLongitude();
lat_loc = location.getLatitude();
lon_loc = location.getLongitude();
circleDraw(lat, lng);
zoomIn(lat, lng);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(20));
try {
currentAddresses = geocoder.getFromLocation(lat_loc, lon_loc, 1);
if (currentAddresses.size() > 0) {
System.out.println("checking : " + "1"
+ currentAddresses.get(0).getCountryName() + "\n" + "2"
+ currentAddresses.get(0).getAddressLine(0) + "3"
+ currentAddresses.get(0).getAddressLine(1) + "4"
+ currentAddresses.get(0).getAddressLine(2));
title.setText(currentAddresses.get(0).getAddressLine(0));
subTitle.setText(currentAddresses.get(0).getAddressLine(1));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void zoomIn(double lat, double lng) {
CameraUpdate center = CameraUpdateFactory
.newLatLng(new LatLng(lat, lng));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(20);
map.moveCamera(center);
map.animateCamera(zoom);
}
private void circleDraw(double lat, double lng) {
map.addCircle(new CircleOptions().center(new LatLng(lat, lng))
.radius(1000).strokeColor(Color.BLACK).strokeWidth(2)
.fillColor(Color.argb(50, 238, 116, 116)));
}
@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
}
}
// Manifest-permissions
<uses-permission android:name="com.zapp.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_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"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
0 comments:
Post a Comment