2016-04-07 13 views
1

誰でもこのコードを教えてください。それはgetMap()getMapAsync(OnMapReadyCallback);に変更し、新しいメソッドOnMapReadyを作成する必要があると言います。私はYouTubeのチュートリアルからステップバイステップを続けます。私は私のプロジェクトでこれを実装する必要があります。もし誰かが私を助けることができたら、私はとても感謝しています。私はアンドロイドアプリの開発者に新しいですが。Googleマップgetmap()> Androidの新しいアップデートでgetMapAsycn()エラーが発生しました

private void setUpMapIfNeeded() { 
    ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
      .getMapAsync(this); 
} 

@Override 
public void onMapReady(final GoogleMap googleMap) { 
    mMap = googleMap; 
    setUpMap(); 
} 
:以下MapActivity.java

private void setUpMapIfNeeded() { 

    if (mMap == null) { 

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

     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

} 

はあなただけonMapReady関数が呼び出されたマップを設定する必要があり

import android.Manifest; 
import android.content.IntentSender; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.app.FragmentActivity; 
import android.util.Log; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 
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 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener, 
     LocationListener, OnMapReadyCallback { 



public static final String TAG = MapsActivity.class.getSimpleName(); 

/* 
* Define a request code to send to Google Play services 
* This code is returned in Activity.onActivityResult 
*/ 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    setUpMapIfNeeded(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 

    // Create the LocationRequest object 
    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
      .setFastestInterval(1 * 1000); // 1 second, in milliseconds 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
    mGoogleApiClient.connect(); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

    if (mGoogleApiClient.isConnected()) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     mGoogleApiClient.disconnect(); 
    } 
} 

private void setUpMapIfNeeded() { 

    if (mMap == null) { 

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

     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 

} 


private void setUpMap() { 
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
} 

private void handleNewLocation(Location location) { 
    Log.d(TAG, location.toString()); 

    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 

    LatLng latLng = new LatLng(currentLatitude, currentLongitude); 

    MarkerOptions options = new MarkerOptions() 
      .position(latLng) 
      .title("I am here!"); 
    mMap.addMarker(options); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    }else{ 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } 
    else { 
     handleNewLocation(location); 
    } 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 

    if (connectionResult.hasResolution()) { 
     try { 
      connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST); 

     } catch (IntentSender.SendIntentException e) { 
      e.printStackTrace(); 
     } 
    } else { 

     Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode()); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    handleNewLocation(location); 
} 


} 

答えて

0

MapActivity.java

の完全なコードです
+0

ありがとうございました..そして、私はいくつかの機能を追加しました。それは仕事です:)もう一度ありがとうございます – Critako

+0

喜んで助けてください:)この解決策があなたの問題を解決した場合、受け入れられた回答/それをアップウィートとしてマークすることを検討してください – antonio

関連する問題