Issue
the fixed location's latitude and longitude are (13.060422000000000000,80.249583000000030000). if my device is with in 20m of that location, it has to show the message like you in the area. if going out, it should show you are going out of place. For now i have created a map and getting the user's location and displaying. But i do not know how to do this. Please help me to do this. My code is MainActivity.java
public class MainActivity extends Activity implements LocationListener{
// static final LatLng TutorialsPoint = new LatLng(13.060422000000000000,
// 80.249583000000030000);
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
//googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = service.getBestProvider(criteria, false);
Location location = service.getLastKnownLocation(provider);
LatLng userLocation = new LatLng(location.getLatitude(),
location.getLongitude());
googleMap.addMarker(new MarkerOptions().position(userLocation)
.title("Adaptavant Technologies"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
CircleOptions options=new CircleOptions().center(userLocation).radius(20).fillColor(0x400099FF).strokeColor(Color.TRANSPARENT).strokeWidth(2);
googleMap.addCircle(options);
} catch (Exception e) {
Log.e("simon", "Error : " + e.getMessage());
e.printStackTrace();
}
}
@Override
public void onLocationChanged(Location arg0) {
Toast.makeText(getApplicationContext(), "You are moving", Toast.LENGTH_SHORT).show();
}
}
and activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simon.learning.googlemaptesting"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
so please tell me how to do this??? Thanks in advance
Solution
You can calculate the distance in two locations on OnLocationChangedListener() with the function -
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB) ;
Answered By - Sumit T
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.