我的目标是跟踪用户的位置,并在到达标记位置时发出提醒。所以首先我得到的位置和标记。然后我得到当前位置并将其与标记位置进行比较。但它没有用。甚至我把标记放在当前位置。它没有给出祝酒信息。这是代码 -
public class MainActivity extends FragmentActivity implements LocationListener{
LocationManager locationManager;
String provider;
ToggleButton toggle;
public static double lat,lang,value1,value2,value3,value4;
public static String strlat,strlat1;
public LatLng loc,loc1;
public double latitude,longitude;
public static String strlang,strlang1 ;
Location location;
public LocationManager locationmanager;
// Google Map
private GoogleMap googleMap;
LocationListener Locationlistener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
final Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 1000, 1, this);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
if(location!=null){
lat = location.getLatitude();
lang = location.getLongitude();
value3 =Double.parseDouble(new DecimalFormat("##.##").format(lat));
value4 =Double.parseDouble(new DecimalFormat("##.##").format(lang));
strlat = String.valueOf(value3);
strlang = String.valueOf(value4);
loc1 =new LatLng(lat, lang);
if((value1 == value3) && (value2 == value4))
{
Toast.makeText(getApplicationContext(), "target reached", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
handler.postDelayed(this, 1000);
}
};
handler.post(runnable);
}
try {
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
// create marker
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
} catch (Exception e) {
e.printStackTrace();
}
googleMap.setOnMapClickListener(new OnMapClickListener(){
public void onMapClick(LatLng point){
Toast.makeText(getBaseContext(),
point.latitude + ", " + point.longitude,
Toast.LENGTH_LONG).show();
latitude = point.latitude;
longitude = point.longitude;
value1 =Double.parseDouble(new DecimalFormat("##.##").format(latitude));
value2 =Double.parseDouble(new DecimalFormat("##.##").format(longitude));
strlat1 = String.valueOf(value1);
strlang1 = String.valueOf(value2);
loc=new LatLng(point.latitude, point.longitude);
MarkerOptions marker = new MarkerOptions().position(loc).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
}
});
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random position around a location for testing purpose only
*/
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
您的代码中存在一些问题。
您无法将坐标与==
进行比较,因为在跟踪设备的位置时,您将无法完全匹配纬度/经度坐标。您需要确定一个阈值,如果设备达到该阈值,则显示警报。
接下来,您没有使用onLocationChanged()
回调,因此您只是在应用启动时查看当前位置。
查看my other answer here中的代码,该代码用于确定设备是否在圈内。这是一个类似的问题。
因此,如果您要将阈值设置为10米,并考虑设备距离目标位置10米范围内达到的目标,则应实现onLocationChanged()
方法,如下所示:
@Override
public void onLocationChanged(Location location) {
float[] distance = new float[2];
Location.distanceBetween( location.getLatitude(), location.getLongitude(),
latitude, longitude, distance);
if( distance[0] < 10 ){
//target location reached
Toast.makeText(getApplicationContext(), "target reached", Toast.LENGTH_SHORT).show();
}
}
答案 1 :(得分:-1)
另外一个daniel.i改变了上面的代码现在它正在工作现在有一些问题。
1.i试图设置启动画面,就像在我的second.class.i中为google map设置了上面的代码。它在运行五秒钟的同时从一个移动到另一个它停止了
new Timer().schedule(new TimerTask(){
public void run() {
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
PendingIntent pendIntent = PendingIntent.getActivity(getBaseContext(), 1000, intent, 0);
}
}, 5000);
2.i想要在谷歌地图中设置搜索如何设置我尝试它不起作用给出一些想法。