2016-11-09 163 views
1

我想在地图上获取当前位置并从当前路径绘制到未知点。绘制路径与步行。我已经集成了谷歌地图,但我不知道如何获取当前位置。我试过这个但没有找到位置。在地图上获取当前位置

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 



public void onMapReady(GoogleMap mMap) { 

    googleMap=mMap; 


    if (googleMap != null) { 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      // TODO: Consider calling 
      // ActivityCompat#requestPermissions 
      // here to request the missing permissions, and then overriding 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
      // int[] grantResults) 
      // to handle the case where the user grants the permission. See the documentation 
      // for ActivityCompat#requestPermissions for more details. 

      try { 
       if (PreferenceClass.getStringPreferences(mContext, Constant.CURRENT_LATITUDE).trim().length() > 0) { 
        LatLng latLng = new LatLng(Double.parseDouble(PreferenceClass.getStringPreferences(mContext, Constant.CURRENT_LATITUDE)), Double.parseDouble(PreferenceClass.getStringPreferences(mContext, Constant.CURRENT_LONGITUDE))); 
        Log.d("latLng", latLng.toString()); 
        googleMap.addMarker(new MarkerOptions().position(latLng).title("My Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.profile))); 
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
        googleMap.setMyLocationEnabled(true); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return; 
     } 
     googleMap.setMyLocationEnabled(true); 
     googleMap.getUiSettings().setRotateGesturesEnabled(true); 
     googleMap.getUiSettings().setZoomControlsEnabled(true); 
     googleMap.getUiSettings().setZoomGesturesEnabled(true); 

     LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, this); 
    } 
} 

public void onLocationChanged(Location location) { 
    googleMap.clear(); 
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.position(latLng); 
    markerOptions.title("My Location"); 
    googleMap.addMarker(markerOptions); 
    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17.0f)); 
} 

回答

0

尝试使用这个类来获取经纬度..

import android.app.AlertDialog.Builder; 
import android.app.Service; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.provider.Settings; 

public class GPSTracker extends Service implements LocationListener { 


private Context context; 

boolean isGPSEnabled=false; 
boolean isNetworkEnabled=false; 
boolean canGetLocation=false; 

Location location; 

double latitude; 
double longitude; 

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES=10; 
private static final long MIN_TIME_BW_UPDATES=1000*60*1; 

protected LocationManager locationManager; 

public GPSTracker(Context context) 
{ 
    this.context=context; 
getLocation(); 
} 

public Location getLocation() 
{ 
    try{ 
    locationManager=(LocationManager) context.getSystemService(LOCATION_SERVICE); 
    isGPSEnabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
    isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 




    if(!isGPSEnabled && !isNetworkEnabled) 
    { 
     showSettingsAlert(); 
    } 
    else{ 
     this.canGetLocation=true; 
     if(isNetworkEnabled) 
     { 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 


     if(locationManager !=null) 
     { 
      location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     if(location !=null) 
     { 
      latitude=location.getLatitude(); 
      longitude=location.getLongitude(); 
     } 
     } 
     } 

     if(isGPSEnabled){ 
      if(location==null) 
      { 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

       if(locationManager !=null) 
       { 
        location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

        if(location !=null) 
        { 
         latitude=location.getLatitude(); 
         longitude=location.getLongitude(); 
        } 

       } 

      } 
     } 


    } 


    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return location; 
} 

public void stopUsingGPS() 
{ 
    if(locationManager !=null) 
    { 
     locationManager.removeUpdates(GPSTracker.this); 
    } 
} 

public double getLatitude() 
{ 
    if(location!=null) 
    { 
     latitude=location.getLatitude(); 
    } 
    return latitude; 

} 


public double getLongitude() 
{ 
    if(location!=null) 
    { 
     longitude=location.getLongitude(); 
    } 
    return longitude; 

} 

public boolean canGetLocation(){ 

return this.canGetLocation; 

} 

public void showSettingsAlert(){ 
    Builder alertDialog=new Builder(context); 
    alertDialog.setTitle("GPS Settings"); 
    alertDialog.setMessage("GPS is not enabled.Do you want to go to the settings menu?"); 
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      context.startActivity(intent); 
     } 
    }); 

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 

     } 
    }); 
    alertDialog.show(); 
} 


@Override 
public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO Auto-generated method stub 
    return null; 
} 


} 

,并将您在使用本地图位置。

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 
GPSTracker gps; 
@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); 
} 



@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 



    try { 

     gps = new GPSTracker(MapsActivity.this); 

     if (gps.canGetLocation) { 
      double latitude = gps.getLatitude(); 
      double longitude = gps.getLongitude(); 
      LatLng sydney = new LatLng(latitude, longitude); 
      mMap.addMarker(new MarkerOptions().position(sydney).title("I am here")); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
      mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 

      for(int i=0;i<MainActivity.nameArray.length;i++) 
      { 
       latitude=Double.parseDouble(MainActivity.latArray[i]); 
       longitude=Double.parseDouble(MainActivity.lonArray[i]); 
       LatLng shops = new LatLng(latitude, longitude); 
       mMap.addMarker(new MarkerOptions().position(shops).title(MainActivity.nameArray[i])); 

      } 





     } else { 
      gps.showSettingsAlert(); 
     } 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 



} 
} 

希望这helps..Good运气

+0

这是获取当前位置,但如何行走时从当前位置上绘制路线。 – user7108398