获取位置和纬度分别为0.0和0.0 [英] Getting Location and latitude as 0.0 and 0.0 respectively

查看:65
本文介绍了获取位置和纬度分别为0.0和0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个在androidhive.com上使用的教程。我跟着那里提到的每一步。我的纬度和经度分别为0.0和0.0 ..请帮助我...



我想通过gps读取用户的当前位置,并希望向当前用户提供其他活动用户位置..我的项目基于通过gps系统进行总线跟踪...请帮助我..

这里发布我的代码...



Dynamic_Map.java

I have gone through a tutorial which was psoted on androidhive.com.I followed each and every step mentioned over there.I'm getting Latitude and Longitude as 0.0 and 0.0 respectively.. please help me...

I want to read current location of user through gps and want provide other active users locations to the current user.. My project is based on bus tracking via gps system... please help me..
here m posting my code...

Dynamic_Map.java

public class Dynamic_Map extends FragmentActivity {
	
	//Google Map
	
	private GoogleMap googleMap;
	GPSTracker gps;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dynamic_map);
		
		try{
			
			gps = new GPSTracker(this);
		  	  
	        // check if GPS enabled     
	  if(gps.canGetLocation()){
	             if(gps.isGPSEnabled){
	           double latitudee = gps.getLatitude();
	           double longitudee = gps.getLongitude();
	             
	            // \n is for new line
	           Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitudee + "\nLong: " + longitudee, Toast.LENGTH_LONG).show();
	             }else{
	            	 Toast.makeText(getApplicationContext(), "Your Location is not available bcos gps is "+gps.isGPSEnabled,Toast.LENGTH_LONG).show();
	            	 gps.showSettingsAlert();
	             }
	        }else{
	            // can't get location
	            // GPS or Network is not enabled
	            // Ask user to enable GPS/network in settings
	         gps.showSettingsAlert();
	       }
			//Load map
			initilizeMap();
			
			
		
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * function to load map.IF map is not created it will create 
	 */
	
	private void initilizeMap(){
		if(googleMap == null){
			googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.dynamic_map)).getMap();
			googleMap.setMyLocationEnabled(true);
			googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(gps.getLatitude(), gps.getLongitude())));
			
			
			
			// latitude and longitude
			//double latitude = 17.3850;
			//double longitude = 78.486671;
			//double latitude = gps.getLatitude();
			//double longitude = gps.getLongitude();
			 
			// create marker
			MarkerOptions marker = new MarkerOptions().position(new LatLng(gps.getLatitude(), gps.getLongitude())).title("Hello Maps");
			 
			// Changing marker icon
			marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.my_maker_icon));
			 
			// adding marker
			googleMap.addMarker(marker);
			
			
			//check if  map is created successfully or not
			if(googleMap == null){
				Toast.makeText(getApplicationContext(), "Sorry not able to create maps", Toast.LENGTH_SHORT).show();
			}
			
			//gps  = new GPSTracker(Dynamic_Map.this);
			
		}
		
	
		// @Override
	   //protected void onResume() {
	    // super.onResume();
	     // initilizeMap();
	   //}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	//	close = false;
		initilizeMap();
	}
	
	
	}



GPSTracker.java


GPSTracker.java

public class GPSTracker extends Service implements LocationListener {

	private final Context mContext;
	private String provider;
	// flag for GPS Status
	boolean isGPSEnabled = false;
	
	//flag for network status
	boolean isNetworkEnabled = false;
	
	boolean canGetLocation = false;
	
	Location location;// location
	double latitude;//latitude
	double longitude;//longitude
	
	//The minimum distance to change updates in meters.
	private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;//10 meters
	
	//The minimum time to change updates in milliseconds
	private static final long MIN_TIME_BW_UPDATES = 1000*60*1; // 1 minute
	
	//Declaring LocationManager 
	protected LocationManager locationManager;
	
	public GPSTracker(Context context){
		this.mContext = context;
		getLocation();
		//onLocationChanged(location);
		
	}
	
	
	public Location getLocation(){
		try{
			locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
			 
			//Criteria criteria = new Criteria();
		    //provider = locationManager.getBestProvider(criteria, true);
		    //Location location = locationManager.getLastKnownLocation(provider);
		    
		    
		   // if (location == null) {
		        // request for a single update, and try again.
		        // Later will request for updates every 10 mins
		    	//locationManager.requestSingleUpdate(criteria, this, null);
		       // location = locationManager
		            //    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
		   // }
		        
			//getting GPS status
			isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
			
			////getting network status
			isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
			
			if(!isGPSEnabled && !isNetworkEnabled){
				//no network provider is enabled
			}else{
				this.canGetLocation = true;
				isGPSEnabled =  locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
				//isGPSEnabled = true;
				//First get location from Network Provider
				if(isNetworkEnabled){
					locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,this);
					Log.d("Network","Network");
				if(locationManager != null){
						location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
						if(location !=null){
							System.out.println("Provider " + provider + " has been selected.");
						      onLocationChanged(location);
							//latitude = location.getLatitude();
							//longitude = location.getLongitude();
						}
					}
					
					}
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return location;
	}
	
	@Override
	public void onLocationChanged(Location location){
		
		
		latitude = location.getLatitude();
		longitude = location.getLongitude();
		
	}
	
	@Override
	public void onProviderDisabled(String provider){
		Toast.makeText(this, "Disabled new provider " + provider,
		        Toast.LENGTH_SHORT).show();
	}
	
	@Override
    public void onProviderEnabled(String provider) {
		Toast.makeText(this, "Enabled new provider " + provider,
		        Toast.LENGTH_SHORT).show();
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
	
   @Override
    public IBinder onBind(Intent arg0){
    	return null;
    }
   
   /**
    * Function to get latitude
    * 
    */
   public double getLatitude(){
	   if(location != null){
		   latitude = location.getLatitude();
		   Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude, Toast.LENGTH_LONG).show();
	   }
	   
	   //return latitude;
	   return latitude;
   }
   
   /**
    * Function to get longitude
    */
   public double getLongitude(){
	   if(location !=null){
		   longitude = location.getLongitude();
		   Toast.makeText(getApplicationContext(), "Your Location is - \nLng: " + longitude, Toast.LENGTH_LONG).show();
	   }
	   
	   //return longitude;
	   return longitude;
	   
   }
   
   /**
    * Function to check if best network provider
    * @return boolean
    */
   public boolean canGetLocation(){
	   return this.canGetLocation;
   }
   
   /**
    * Function to show settings alert dialog
    */
   public void showSettingsAlert(){
	   AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
	   
	   //Setting Dialog Title
	   alertDialog.setTitle("GPS is settings");
	   
	   //Setting Dialog Message
	   alertDialog.setMessage("GPS is not enabled.Do you want to go to settings menu?");
	   
	   //On pressing Settings button 
	   alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
		   public void onClick(DialogInterface dialog,int which){
			   Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			   mContext.startActivity(intent);
			   
		   }
	   });
	   
	   //On pressing cancel button
	   alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		   public void onClick(DialogInterface dialog,int which){
			   dialog.cancel();
		   }
				
	   });
	   
	   //Showing Alert Message
	   alertDialog.show();
	     
   }
   
   /**
    * Stop using GPS listener
    * Calling this function will stop using GPS in app
    * 
    */
	
   public void stopUsingGPS(){
	   if(locationManager != null){
		   locationManager.removeUpdates(GPSTracker.this);
		   }
  }
   }







我已包含必要AndroidManifest文件中的权限...






I have included necessary permissions in AndroidManifest file...

<!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
       <uses-permission android:name="android.permission.INTERNET" />



请帮助我....这是我的最后一年项目..请帮助



谢谢:)


Please help me.... this is my final year project.. please help

thank you :)

推荐答案

这篇关于获取位置和纬度分别为0.0和0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆