2016-06-16 10 views
1

これはStack Overflowでこの問題に関して投稿した最初の質問ではありません。マップフラグメントを現在の場所にズームするには、本当に助けが必要です。私は何も試していないとは思わないでください。私はたくさん試しました。Androidマップのフラグメントを現在の位置にズームできません

現在の場所ではなく別の場所にマップズームを試しても、現在の場所とにズームする必要があります。ここで何が起きているのかは考えられません。ここで

は私GoogleMapsFragment.java

public class GoogleMapsFragment extends android.support.v4.app.Fragment implements LocationListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

    View rootView; 
    private GoogleApiClient mGoogleApiClient; 
    private GoogleMap map; 
    private LatLng latlng; 

    public GoogleMapsFragment() 
    { 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     if(!isGooglePlayServiceAvailable()) 
     { 
      return null; 
     } 

     if(rootView != null) 
     { 
      ViewGroup parent = (ViewGroup)rootView.getParent(); 
      if(parent != null) 
      { 
       parent.removeView(rootView); 
      } 
     } 
     else 
     { 
      rootView = inflater.inflate(R.layout.google_maps, container, false); 
      map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 

      SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
        .findFragmentById(R.id.map); 

      if (mapFragment != null) 
       mapFragment.getMapAsync(this); 

      if (mGoogleApiClient == null) { 
       mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
         .addApi(LocationServices.API).addConnectionCallbacks(this) 
         .addOnConnectionFailedListener(this).build(); 
       mGoogleApiClient.connect(); 
      } 
     } 

     return rootView; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     LatLng latLng = new LatLng(latitude, longitude); 

     map.addMarker(new MarkerOptions().position(latLng)); 
     map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     map.animateCamera(CameraUpdateFactory.zoomTo(15)); 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

    private boolean isGooglePlayServiceAvailable() 
    { 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()); 
     if(ConnectionResult.SUCCESS == status) 
     { 
      return true; 
     } 
     else 
     { 
      GooglePlayServicesUtil.getErrorDialog(status, getActivity(), 0).show(); 
      return false; 
     } 
    } 

    //Zoom to the current location 
    public Location getMyLocation() { 
     LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 
     if (location != null) 
     { 
      map.animateCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(location.getLatitude(), location.getLongitude()), 13)); 

      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude())) // 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 
      map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     } 

     return location; 
    } 

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

     // Zoom in, animating the camera. 
     map.animateCamera(CameraUpdateFactory.zoomTo(5), 5000, null); 
     map.getUiSettings().setZoomControlsEnabled(false); 
     map.getUiSettings().setCompassEnabled(false); 
     map.getUiSettings().setMyLocationButtonEnabled(true); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 

     LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
     } 
    } 
} 

である私はドーム小さなミスをしたと思うし、それが別の場所にズームすると、それは私の国にも、近くではありません。

私のズーム位置の画像です。

Screen Shot of the Map

誰かがどのように現在の場所に私の地図の断片をズームを教えてください。

何か助けていただければ幸いです。 :)

〜onMapReady内部のGaurav〜

追加getMyLocation()()の編集が

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

     getMyLocation(); 

     // Zoom in, animating the camera. 
     map.animateCamera(CameraUpdateFactory.zoomTo(5), 5000, null); 
     map.getUiSettings().setZoomControlsEnabled(false); 
     map.getUiSettings().setCompassEnabled(false); 
     map.getUiSettings().setMyLocationButtonEnabled(true); 


    } 

〜編集した完全なコード〜

public class GoogleMapsFragment extends android.support.v4.app.Fragment implements LocationListener, OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

    View rootView; 
    private GoogleApiClient mGoogleApiClient; 
    private GoogleMap map; 
    private LatLng latlng; 

    public GoogleMapsFragment() 
    { 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     if(!isGooglePlayServiceAvailable()) 
     { 
      return null; 
     } 

     if(rootView != null) 
     { 
      ViewGroup parent = (ViewGroup)rootView.getParent(); 
      if(parent != null) 
      { 
       parent.removeView(rootView); 
      } 
     } 
     else 
     { 
      rootView = inflater.inflate(R.layout.google_maps, container, false); 
      map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 

      SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager() 
        .findFragmentById(R.id.map); 

      if (mapFragment != null) 
       mapFragment.getMapAsync(this); 

      if (mGoogleApiClient == null) { 
       mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
         .addApi(LocationServices.API).addConnectionCallbacks(this) 
         .addOnConnectionFailedListener(this).build(); 
       mGoogleApiClient.connect(); 
      } 
     } 

     return rootView; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 

     LatLng latLng = new LatLng(latitude, longitude); 

     map.addMarker(new MarkerOptions().position(latLng)); 
     map.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     map.animateCamera(CameraUpdateFactory.zoomTo(15)); 
    } 

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

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 

    private boolean isGooglePlayServiceAvailable() 
    { 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()); 
     if(ConnectionResult.SUCCESS == status) 
     { 
      return true; 
     } 
     else 
     { 
      GooglePlayServicesUtil.getErrorDialog(status, getActivity(), 0).show(); 
      return false; 
     } 
    } 

    //Zoom to the current location 
    public Location getMyLocation() { 
     LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 
     if (location != null) 
     { 
      map.animateCamera(CameraUpdateFactory.newLatLngZoom(
        new LatLng(location.getLatitude(), location.getLongitude()), 13)); 

      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(location.getLatitude(), location.getLongitude())) // 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 
      map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

     } 

     return location; 
    } 

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

     Location myLocation = getMyLocation(); 

     LatLng latLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude()); 
     map.animateCamera(CameraUpdateFactory.newLatLngZoom(
       latLng 
       , 15)); 

    } 

    private LocationRequest mLocationRequest; 
    private static final long INTERVAL = 1000 * 1000; 
    private static final long FASTEST_INTERVAL = 1000 * 900; 

    @Override 
    public void onConnected(Bundle bundle) { 

     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(INTERVAL); 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this); 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     if (mGoogleApiClient != null) { 
      mGoogleApiClient.connect(); 
     } 
    } 
} 

〜エラーログ〜

06-16 11:23:28.180 6647-6647/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.myayubo, PID: 6647 
    java.lang.NullPointerException 
      at com.myayubo.GoogleMapsFragment.onMapReady(GoogleMapsFragment.java:151) 
      at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 
      at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source) 
      at android.os.Binder.transact(Binder.java:361) 
      at com.google.android.gms.maps.internal.v$a$a.a(:com.google.android.gms.alldynamite:82) 
      at maps.ei.bu$6.run(Unknown Source) 
      at android.os.Handler.handleCallback(Handler.java:808) 
      at android.os.Handler.dispatchMessage(Handler.java:103) 
      at android.os.Looper.loop(Looper.java:193) 
      at android.app.ActivityThread.main(ActivityThread.java:5299) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
      at dalvik.system.NativeStart.main(Native Method) 

答えて

2

ここにあります:

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

     Location myLocation = getMyLocation(); 

LatLng latLng = new LatLng(myLocation.getlatitude(), myLocation.getLongitude()); 
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
       latLng 
       , 15)); 
} 

private LocationRequest mLocationRequest; 
    private static final long INTERVAL = 1000 * 1000; 
    private static final long FASTEST_INTERVAL = 1000 * 900; 
@Override 
    public void onConnected(Bundle bundle) { 

     mLocationRequest = new LocationRequest(); 
       mLocationRequest.setInterval(INTERVAL); 
       mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
       mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

    } 

これはonLocationChanged()を呼び出します。

+0

何も起こりませんでした。 : –

+0

ああ、そのズームを取得して間違った場所に。右 – Gaurav

+0

ええ、 'getMyLocation()'も呼び出していないズームの部分が起こっている 'onMapReady()'私は何をするのか分からない –

0

てみてください。これはあなたのために有用である可能性があり、この1

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15)); 

を使用してみてくださいを受けた場合、場所は座標値、あなたが受けているかどうかを確認するかをまず

+0

試してみました。運がない。 :( –

0

getMyLocation()内から、onLocationChanged()を呼び出します。

関連する問題