2016-11-09 5 views
1

I aktuelle Position auf der Karte haben möchten Route mit walking.I unbekannter point.Draw Route von Strom zu ziehen haben google map integriert, aber ich do't wissen, wie aktuelle location.I zu bekommen versucht, diese fand aber keinen Standort.Get Current Standort auf der Karte

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)); 
} 

Antwort

0

Versuchen Sie, diese Klasse mit der Breite und Länge zu bekommen ..

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; 
} 


} 

Und in der Karte markieren Sie Ihre Position diese verwenden.

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(); 
    } 



} 
} 

Hope this helps..Good Glück

+0

Dies ist für immer aktuellen Standort ist, aber wie Route von der aktuellen Position zu ziehen, beim Gehen. – user7108398

Verwandte Themen