2016-04-07 6 views
-1

私自身のアンドロイドアプリでGoogleマップを表示したい...私はgetMapAsync()を使用してmanifest.imでgoogle map apiキーを使用しましたが、動作しません。 コードgetMapAsyncを使用して自分のアンドロイドアプリでGoogleマップを表示する方法

ここ

... はありがとう..以下である私は私を助けdone.pleaseた間違いがあったことは、XML

ここ
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/map" 
      tools:context=".ClinicMap" 
      class="com.google.android.gms.maps.SupportMapFragment" 
      android:apiKey="AIzaSyCbtSjGcG1g2u9H-hfuO6Tx7F442ZndDb4"/> 

が私の主な活動は

です
public class ClinicMap extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

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


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

//  MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
//  mapFragment.getMapAsync(this); 
} 

@TargetApi(Build.VERSION_CODES.M) 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    // Add a marker in Mumbai, India, and move the camera. 
    LatLng mumbai = new LatLng(19.0128, 72.8246); 
    if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // public void requestPermissions(@NonNull String[] permissions, int requestCode) 
     // 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 Activity#requestPermissions for more details. 
     return; 
    } 
    mMap.setMyLocationEnabled(true); 
    //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(mumbai)); 
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mumbai, 4)); 
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    mMap.addMarker(new MarkerOptions() 
      .title("mumbai") 
      .snippet("The most populous city in India.") 
      .position(mumbai)); 
    } 
} 

MapFragmentを使用したときにエラーが発生しました "仮想メソッド 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)を呼び出しようとしました'がnullになっていますオブジェクト参照」

私はSupportMapFragment
output will be like these

+0

あなたが任意のエラーを取得しているここhttp://stackoverflow.com/questions/27549521/googlemap-not-showing-the-changes-i-made –

+0

からの例に従ってください? catlogを投稿する –

+0

いいえ。@ Ravi Vaghela – Pratik

答えて

0

を使用する場合、キーはあなたがアクセスすることができませんマニフェストファイルにない場合は、ないレイアウトファイルにたManifest.xmlであなたのAPIキーを配置する必要がありますあなたのフラグメントには何も表示されません。

マニフェストに移動し、次の行が追加されているかどうかを確認します。

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true"/> 

とINSIDE <application>タグ:

<meta-data 
android:name="com.google.android.gms.version" 
android:value="@integer/google_play_services_version" /> 

<meta-data 
android:name="com.google.android.maps.v2.API_KEY" 
android:value="API_KEY"/> 

また、あなたがあなたのmapFragmentオブジェクトのチェックする必要がありますが正しく作成され、通常、私は私がnullを渡していないよということを確認するために、コードブロックを使用getMapAsync()を参照してください。

fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_fragment); 
     if(fragment == null){ 
      FragmentManager manager = getFragmentManager(); 
      fragment =SupportMapFragment.newInstance(); 
      manager.beginTransaction().replace(R.id.map_fragment, fragment).commit(); 

     } 
     if (fragment != null){ 
      fragment.getMapAsync(this); 
     } 

私はシンプルなフラグメントでこれを使用していたあなたは多分あなたの状況にそれに合わせていくつかの小さな修正を行う必要があるので、あなたはFragmentActivityを持っています。

EDIT:

私はあなたのクラスがFragmentActivity、私は断片を拡張するクラスで作品を書いたコードを拡張していることに気付きました。

  1. は、作成し、マップを操作extends Fragmentimplements OnMapReadyCallbackFragmentActivityと別のクラスを作成します。私の心に は、私は2つの可能な解決策を持っています。

  2. 単純なアクティビティを作成するには、layout.xmlでフラグメントを作成し、マップをインスタンス化します。この場合、 は、あなたがこのような何かを持っている必要があります。

MAP_LAYOUT.xml:

<FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <fragment 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/map" 
     tools:context=".MainActivity" 
     android:name="com.google.android.gms.maps.SupportMapFragment" /> 

</FrameLayout> 

ACTIVITY.java:私はOnCreate()SetUpMapIfYouNeed()SetUpMap()の唯一の詳細を記述します

、クラス全体ではありません。

private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.map_activity); 

     Bundle bundle = new Bundle(getIntent().getExtras()); 
     this.title = bundle.getString("name"); 
     setUpMapIfNeeded(); 
     //Other lines of code 
     //... 
     //... 
     } 


/** 
    * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
    * installed) and the map has not already been instantiated.. This will ensure that we only ever 
    * call {@link #setUpMap()} once when {@link #mMap} is not null. 
    * <p/> 
    * If it isn't installed {@link SupportMapFragment} (and 
    * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
    * install/update the Google Play services APK on their device. 
    * <p/> 
    * A user can return to this FragmentActivity after following the prompt and correctly 
    * installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
    * have been completely destroyed during this process (it is likely that it would only be 
    * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
    * method in {@link #onResume()} to guarantee that it will be called. 
    */ 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

/** 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, we 
    * just add a marker near Africa. 
    * <p/> 
    * This should only be called once and when we are sure that {@link #mMap} is not null. 
    */ 
    private void setUpMap() { 
     //mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 

     mMap.setMyLocationEnabled(true); 
     //Move the map to a specific point 


    } 
+0

私はマニフェストにもapiキーを追加しました。 – Pratik

+0

まだ空のマップがありますか、nullオブジェクト参照エラーがありますか? – Marco

+0

はい..空のマップのみ.. – Pratik

関連する問題