在开始时获取当前位置 [英] get current location on start

查看:94
本文介绍了在开始时获取当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用gps显示用户当前位置。在启动时,它会在海中显示一些随机位置,当我点击到GPS图标时,它会显示我当前的位置(带有光纤图标的白色圆圈)。
如何在启动时显示该信息?



我的代码是:



<$ p我是新的android / java版本,所以请随时纠正我的错误。 $ p> public class MainMapsActivity extends FragmentActivity implements android.location.LocationListener {

GoogleMap googleMap;


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_maps);


SupportMapFragment fm =(SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map);


googleMap = fm.getMap();


googleMap.setMyLocationEnabled(true);



LocationManager locationManager =(LocationManager)this.getSystemService(LOCATION_SERVICE);


标准标准=新标准();


字符串提供者= locationManager.getBestProvider(criteria,true);


位置位置= locationManager.getLastKnownLocation(provider);

if(location!= null){
onLocationChanged(location);
}



locationManager.requestLocationUpdates(provider,20000,0,this);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));


$ b @Override
public void onLocationChanged(Location location){

TextView tvLocation =(TextView)findViewById(R.id .tv_location);


double latitude = location.getLatitude();


double longitude = location.getLongitude();


LatLng latLng =新LatLng(纬度,经度);


googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));


tvLocation.setText(Latitude:+ latitude +,Longitude:+ longitude);


$ b


解决方案

创建位置类的数据成员。

  public static Location mLocation; 

在地图上使用OnMyLocationChangeListener

  mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener(){
@Override
public void onMyLocationChange(Location location){
mLocation = location;
displayCurrentLocation( );
}
});

public void displayCurrentLocation(){
CameraPosition cameraPosition = new CameraPosition.Builder()。
target(new LatLng(mLocation.getLatitude(),mLocation.getLongitude()))。
tilt(60)。
zoom(15)。
轴承(0)。
build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

希望它能起作用。


I need to display users current locations using gps. At startup it displays some random location in sea and when i'll click to gps icon it displays my current location (white circle with optic icon). How can I do to display that on startup? I'm new at android/java so feel free to correct me anything.

my code is:

public class MainMapsActivity extends FragmentActivity implements android.location.LocationListener {

    GoogleMap googleMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_maps);


        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);


        googleMap = fm.getMap();


        googleMap.setMyLocationEnabled(true);



        LocationManager locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);


        Criteria criteria = new Criteria();


        String provider = locationManager.getBestProvider(criteria, true);


        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
            onLocationChanged(location);
        }



        locationManager.requestLocationUpdates(provider, 20000, 0, this);
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    }


    @Override
    public void onLocationChanged(Location location) {

        TextView tvLocation = (TextView) findViewById(R.id.tv_location);


        double latitude = location.getLatitude();


        double longitude = location.getLongitude();


        LatLng latLng = new LatLng(latitude, longitude);


        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));


        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );

    }
}

解决方案

Create a data member of Location class.

  public static Location mLocation;

use OnMyLocationChangeListener on Map

   mMap.setOnMyLocationChangeListener(new   GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            mLocation = location;
                displayCurrentLocation();
        }
    });

      public  void displayCurrentLocation(){
    CameraPosition cameraPosition = new CameraPosition.Builder().
            target(new LatLng(mLocation.getLatitude(), mLocation.getLongitude())).
            tilt(60).
            zoom(15).
            bearing(0).
            build();
       mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

hope it will works.

这篇关于在开始时获取当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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