Showing current location on android Google Map v2, But app crashes

后端 未结 2 1757
猫巷女王i
猫巷女王i 2021-01-26 10:05

Here is my Activity code

package com.example1.heartconnect;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentAc         


        
相关标签:
2条回答
  • 2021-01-26 10:30

    Location loc = map.getMyLocation(); didn't wait for your current location

    See the example: http://developer.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/

    Also see this: http://developer.android.com/training/location/retrieve-current.html

    0 讨论(0)
  • 2021-01-26 10:40

    This is the code for getting current location on the map.

    public class ShowMap extends FragmentActivity implements LocationListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show_location_map);
    
            getMap();
    
      }
      public void getMap(){
    
          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);
            }
        }
    
    
       @Override
       public void onLocationChanged(Location 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));
    
    
       }
    
       @Override
       public void onProviderDisabled(String arg0) {
          // TODO Auto-generated method stub
    
       }
    
       @Override
       public void onProviderEnabled(String arg0) {
           // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
           // TODO Auto-generated method stub
    
      }
    

    and you need to import google play store into your project as a library. and add support v4 into your project like Right click your project -> select properties ->java build path -> select Libraries Tab -> click Add External JARs, and give path of your support v4 jar.

    I am sure this step will help you.

    0 讨论(0)
提交回复
热议问题