2016-04-25 8 views
2

私は苦労してGoogleマップの現在の場所を取得して、私はまた働いていない公式マップのデモを試みました。右上にgetmylocationボタンが表示されていますが、マップ上に現在の場所である青い点が表示されませんでした。 ここに私のコードがあります。私はエミュレータのターゲットapi 23を使っています。私はgithubから直接ダウンロードしたコードからエミュレータの問題かもしれないと思います。 アドバイスをいただければ幸いです。 [スクリーンショット] [1]Androidの地図を表示私の場所は動作しませんでした

package com.example.mapdemo; 
    import com.google.android.gms.maps.GoogleMap; 
    import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener; 
    import com.google.android.gms.maps.OnMapReadyCallback; 
    import com.google.android.gms.maps.SupportMapFragment; 

    import android.Manifest; 
    import android.content.pm.PackageManager; 
    import android.os.Bundle; 
    import android.support.annotation.NonNull; 
    import android.support.v4.app.ActivityCompat; 
    import android.support.v4.content.ContextCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.widget.Toast; 

    /** 
    * This demo shows how GMS Location can be used to check for changes to the users location. The 
    * "My Location" button uses GMS Location to set the blue dot representing the users location. 
    * Permission for {@link android.Manifest.permission#ACCESS_FINE_LOCATION} is requested at run 
    * time. If the permission has not been granted, the Activity is finished with an error message. 
    */ 
    public class MyLocationDemoActivity extends AppCompatActivity 
      implements 
      OnMyLocationButtonClickListener, 
      OnMapReadyCallback, 
      ActivityCompat.OnRequestPermissionsResultCallback { 

     /** 
     * Request code for location permission request. 
     * 
     * @see #onRequestPermissionsResult(int, String[], int[]) 
     */ 
     private static final int LOCATION_PERMISSION_REQUEST_CODE = 1; 

     /** 
     * Flag indicating whether a requested permission has been denied after returning in 
     * {@link #onRequestPermissionsResult(int, String[], int[])}. 
     */ 
     private boolean mPermissionDenied = false; 

     private GoogleMap mMap; 

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

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

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

      mMap.setOnMyLocationButtonClickListener(this); 
      enableMyLocation(); 
     } 

     /** 
     * Enables the My Location layer if the fine location permission has been granted. 
     */ 
     private void enableMyLocation() { 
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
        != PackageManager.PERMISSION_GRANTED) { 
       // Permission to access the location is missing. 
       PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE, 
         Manifest.permission.ACCESS_FINE_LOCATION, true); 
      } else if (mMap != null) { 
       // Access to the location has been granted to the app. 
       mMap.setMyLocationEnabled(true); 
      } 
     } 

     @Override 
     public boolean onMyLocationButtonClick() { 
      Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show(); 
      // Return false so that we don't consume the event and the default behavior still occurs 
      // (the camera animates to the user's current position). 
      return false; 
     } 


     @Override 
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
       @NonNull int[] grantResults) { 
      if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) { 
       return; 
      } 

      if (PermissionUtils.isPermissionGranted(permissions, grantResults, 
        Manifest.permission.ACCESS_FINE_LOCATION)) { 
       // Enable the my location layer if the permission has been granted. 
       enableMyLocation(); 
      } else { 
       // Display the missing permission error dialog when the fragments resume. 
       mPermissionDenied = true; 
      } 
     } 

     @Override 
     protected void onResumeFragments() { 
      super.onResumeFragments(); 
      if (mPermissionDenied) { 
       // Permission was not granted, display error dialog. 
       showMissingPermissionError(); 
       mPermissionDenied = false; 
      } 
     } 

     /** 
     * Displays a dialog with error message explaining that the location permission is missing. 
     */ 
     private void showMissingPermissionError() { 
      PermissionUtils.PermissionDeniedDialog 
        .newInstance(true).show(getSupportFragmentManager(), "dialog"); 
     } 

    } 

**strong text** 
    [1]: http://i.stack.imgur.com/YDhk2.png 

答えて

1

初めてのエミュレータ用の新鮮なイメージを使用している場合があり、デバイスに保存された任意の位置座標ではない、それはあなたのコンピュータの場所を使用していません。

右側のサイドバーの[その他]オプションをクリックすると、[拡張コントロール]ウィンドウが開きます。 [場所]タブ(デフォルトで選択する必要があります)を選択し、緯度と経度の入力を心の欲求に変更して[送信]をクリックします。 Voilà!

関連する問題