2017-08-21 2 views
-2

私はマップを使用しているアプリケーションを開発しています。アプリケーションを開くとすぐに地図上に自分の位置を表示する必要がありますが、現時点では地図は表示されていますが、現在の位置は自動的に表示されません。右上のMyLocationButtonをクリックすると表示されます。それでは、どのように私のコードでは、上記の機能を組み込むために私のアプリケーションを開くと自動的に私の現在の場所に地図を表示する方法は?

答えて

0

プロジェクト内のクラスを作成し、

このコードをコピー
import android.app.Service; 
import android.content.Context; 
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.support.annotation.Nullable; 
import android.util.Log; 



public class AppLocationService extends Service implements LocationListener { 
protected LocationManager locationManager; 
Location location; 
private static final long min_distance_forupdate = 10; 
private static final long min_time_to_update = 2 * 60 * 1000; 

public AppLocationService(Context context) { 
    locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE); 


} 

public Location getLocation(String provider) { 
    if (locationManager.isProviderEnabled(provider)) { 

try { 

locationManager.requestLocationUpdates(provider, min_time_to_update, min_distance_forupdate, this); 
if (locationManager != null) { 
    location = locationManager.getLastKnownLocation(provider); 
    return location; 

} 
}catch (SecurityException r){ 

    Log.d("loc",r.getMessage()); 
} 
      return location; 




    } 

return location; 
} 

@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

@Override 
public void onLocationChanged(Location location) { 

} 

@Override 
public void onStatusChanged(String s, int i, Bundle bundle) { 

} 

@Override 
public void onProviderEnabled(String s) { 

} 

@Override 
public void onProviderDisabled(String s) { 

} 
} 

その後、onMapReady機能を使用しているあなたのマップの活動でこのコード

mMap = googleMap; 
    appLocationService = new AppLocationService(MapsActivity.this); 


    Location newlocation =  appLocationService.getLocation(LocationManager.NETWORK_PROVIDER); 

    if (newlocation != null) { 
     double curlat = newlocation.getLatitude(); 
     double curlog = newlocation.getLongitude(); 

     Log.d("latitude", curlat + " ----" + curlog); 



     LatLng sydney = new LatLng(curlat, curlog); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Your location")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
     LatLng coordinate = new LatLng(curlat, curlog); 
     CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5); 
     mMap.animateCamera(yourLocation); 

     CameraPosition cameraPosition = new CameraPosition.Builder() 

       .target(new LatLng(curlat, curlog))  // Sets the center of the map to location user 

       .zoom(17)     // Sets the zoom 
       .bearing(90)    // Sets the orientation of the camera to east 
       .tilt(40)     // Sets the tilt of the camera to 30 degrees 
       .build();     // Creates a CameraPosition from the builder 
     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
+0

ありがとうたくさんの友人 –

+0

問題がある場合は私の質問に答えてください – Hami

+0

答えを記入plz – Hami

関連する問題