如何获得我的位置?

时间:2017-11-14 20:09:19

标签: java android

我是新的android中的地图我有这个代码:

enter codepublic class MapsActivity extends FragmentActivity implements GoogleMap.OnMyLocationButtonClickListener,
    GoogleMap.OnMyLocationClickListener,
    OnMapReadyCallback {

private GoogleMap mMap;
Button req_button;
String[] lat;
String[] lon;
String[] id;
String[] emails;
double latitude,lonitude;
int result_search,StatusChange_customer;
String params_search ,Status_cheked_customer,params_checked_customer,Status_cheked_customer_rating;
public Handler mHandler;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Intent intent1 = getIntent();
    lat = intent1.getStringArrayExtra("latitute");
    lon  = intent1.getStringArrayExtra("longitude");
    id = intent1.getStringArrayExtra("id");
    emails  = intent1.getStringArrayExtra("emails");
    latitude=intent1.getDoubleExtra("MyLat",0);
    lonitude=intent1.getDoubleExtra("MyLon",0);
    req_button =(Button)findViewById(R.id.R_id);
    this.mHandler = new Handler();

    this.mHandler.postDelayed(m_Runnable,5000);

}

private final Runnable m_Runnable = new Runnable()
{
    public void run()

    {

        Search_for_Accepted_Job();
        Toast.makeText(MapsActivity.this,"in runnable",Toast.LENGTH_SHORT).show();

        MapsActivity.this.mHandler.postDelayed(m_Runnable, 10000);
    }

};

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng [] sydeny = new LatLng[lat.length];
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
    // Add a marker in Sydney and move the camera
    for (int i=0 ; i< lat.length;i++) {
        sydeny[i] = new LatLng(Double.parseDouble(lat[i]), Double.parseDouble(lon[i]));

    }
    for (int i=0 ; i< sydeny.length;i++) {
        mMap.addMarker(new MarkerOptions().position(sydeny[i]).title(emails[i]+ " " + id[i]));
      //  mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydeny[i], 5));
    }
   LatLng sydeny1 = new LatLng(latitude,lonitude);
    mMap.addMarker(new MarkerOptions().position(sydeny1).title("Your location" ));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydeny1, 5));*/

   mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationButtonClickListener(this);
    mMap.setOnMyLocationClickListener(this);



}



public void Request(View view) {

    Toast.makeText(this," Request",Toast.LENGTH_SHORT).show();
  for (int i=0; i< lat.length;i++)
    {

            send_request(id[i]);

    }
    req_button.setEnabled(false);
}


@Override
public boolean onMyLocationButtonClick() {
    Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
    return false;
}

@Override
public void onMyLocationClick(@NonNull Location location) {
    Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
    lonitude=location.getLatitude();
    lonitude=location.getLongitude();
} 

因此,此代码从另一个活动中获取经度和经度,并将它们呈现在Map中,最近我尝试实现两个接口       (GoogleMap.OnMyLocationButtonClickListener         GoogleMap.OnMyLocationClickListener) 我在地图中用蓝色圆圈获取我的位置并与我实时更改,并且有一个名为

的方法
 public void onMyLocationClick(@NonNull Location location) {
    Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show(); 

我的问题是如何使用这种方法的纬度和经度,我的意思是如何获得我的位置的纬度和经度,因为它是正确的我需要它来计算我和其他地方之间的距离。

1 个答案:

答案 0 :(得分:0)

要在您的应用中正确使用位置,您需要处理要求用户在设置中打开位置的权限(如果已关闭),然后按照文档中的步骤操作: https://developer.android.com/training/location/retrieve-current.html

要准确回答您的问题,您是否尝试location.getLatitude()location.getLongitude()