【问题标题】:Google Maps API v2 tracking phoneGoogle Maps API v2 追踪电话
【发布时间】:2014-06-22 19:43:24
【问题描述】:

我正在创建一个应用程序,我只是让它在屏幕上显示地图。现在我想知道如何让它找到用户并跟踪他/她的去向。我做了setMyLocationEnabled(true),但所做的只是显示一个按钮,当你点击它时,什么也没有发生。之前没用过google maps api,所以不太了解。

【问题讨论】:

    标签: java android google-maps google-maps-api-2


    【解决方案1】:

    你有这个麻烦很好解释 - http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/

    您必须实现从 Google Maps API 获取用户位置。您需要使用这些权限:

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    

    有一个类的例子表明,你想要什么。从我上面写的页面得到这个:

    public class MainActivity extends FragmentActivity implements LocationListener {
    
        GoogleMap googleMap;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Getting Google Play availability status
            int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
    
            // Showing status
            if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
    
                int requestCode = 10;
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
                dialog.show();
    
            }else { // Google Play Services are available
    
                // Getting reference to the SupportMapFragment of activity_main.xml
                SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    
                // Getting GoogleMap object from the fragment
                googleMap = fm.getMap();
    
                // Enabling MyLocation Layer of Google Map
                googleMap.setMyLocationEnabled(true);
    
                // Getting LocationManager object from System Service LOCATION_SERVICE
                LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
                // Creating a criteria object to retrieve provider
                Criteria criteria = new Criteria();
    
                // Getting the name of the best provider
                String provider = locationManager.getBestProvider(criteria, true);
    
                // Getting Current Location
                Location location = locationManager.getLastKnownLocation(provider);
    
                if(location!=null){
                    onLocationChanged(location);
                }
                locationManager.requestLocationUpdates(provider, 20000, 0, this);
            }
        }
        @Override
        public void onLocationChanged(Location location) {
    
            TextView tvLocation = (TextView) findViewById(R.id.tv_location);
    
            // Getting latitude of the current location
            double latitude = location.getLatitude();
    
            // Getting longitude of the current location
            double longitude = location.getLongitude();
    
            // Creating a LatLng object for the current location
            LatLng latLng = new LatLng(latitude, longitude);
    
            // Showing the current location in Google Map
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    
            // Zoom in the Google Map
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
    
            // Setting latitude and longitude in the TextView tv_location
            tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-24
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多