固定地点的经度和纬度为(13.060422000000000000,80.249583000000030000)。如果我的设备位于该位置的20米处,则必须在该区域显示与您类似的消息。如果外出,它应该表明你不合适。现在我创建了一个地图并获取用户的位置并显示。但我不知道该怎么做。请帮我这样做。我的代码是 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();
}
}
和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>
所以请告诉我怎么做?提前致谢
答案 0 :(得分:1)
您可以使用函数计算OnLocationChangedListener()上两个位置的距离 -
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) ;
答案 1 :(得分:-2)
您可以在OnLocationChanged Listener上计算从当前位置到指定位置的距离,然后就可以显示吐司。