2016-03-31 12 views
0

現在の場所を取得できません。これは私のコードです:アンドロイドスタジオの現在の場所を取得できません

マーカーは他の場所に表示されていますが、現在の位置に表示されていません。 私のXMLは次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.app.MainActivity" 
    tools:ignore="MergeRootFrame" > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <fragment 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:name="com.google.android.gms.maps.MapFragment" 
      android:id="@+id/map" /> 
    </LinearLayout> 
</FrameLayout> 

そして、これは私のクラスである:

public class MapsActivity extends AppCompatActivity implements LocationListener { 

    private GoogleMap map; 
    String strAdd; 
    Location location; 
    double latitude, longitude; 


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


     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

     map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
     map.setMyLocationEnabled(true); 
     map.getUiSettings().setZoomControlsEnabled(true); 
     map.getUiSettings().setCompassEnabled(true); 
     map.getUiSettings().setMyLocationButtonEnabled(true); 
     map.getUiSettings().setZoomGesturesEnabled(true); 
     map.getUiSettings().setRotateGesturesEnabled(true); 

     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     List<String> providers = lm.getProviders(true); 
     Location l = null; 

     for (int i = 0; i < providers.size(); i++) { 
      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       // TODO: Consider calling 
       // ActivityCompat#requestPermissions 
       // 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 ActivityCompat#requestPermissions for more details. 
       l = lm.getLastKnownLocation(providers.get(i)); 
       return; 
      } 

      if (l != null) { 
       latitude = l.getLatitude(); 
       longitude = l.getLongitude(); 
       strAdd = getCompleteAddressString(latitude, longitude); 
       break; 
      } 
     } 

     if (map != null) { 

      MarkerOptions marker = new MarkerOptions().position(
        new LatLng(latitude, longitude)).title("Hello Maps").snippet("Discription"); 

      marker.icon(BitmapDescriptorFactory 
        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)); 

      // Moving Camera to a Location with animation 
      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(new LatLng(latitude, longitude)).zoom(12).build(); 

      map.animateCamera(CameraUpdateFactory 
        .newCameraPosition(cameraPosition)); 

      map.addMarker(marker); 

     } 
    } 



    private String getCompleteAddressString(double LATITUDE, double LONGITUDE) { 
     String strAdd = ""; 
     Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
     try { 
      List<android.location.Address> addresses = geocoder 
        .getFromLocation(LATITUDE, LONGITUDE, 1); 
      if (addresses != null) { 
       android.location.Address returnedAddress = addresses.get(0); 
       StringBuilder strReturnedAddress = new StringBuilder(""); 

       for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { 
        strReturnedAddress 
          .append(returnedAddress.getAddressLine(i)).append(
          "\n"); 
       } 
       strAdd = strReturnedAddress.toString(); 
       Log.w("My Current loction address", 
         "" + strReturnedAddress.toString()); 
      } else { 
       Log.w("My Current loction address", "No Address returned!"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.w("My Current loction address", "Canont get Address!"); 
     } 
     return strAdd; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     if (map != null) 
     { 
      drawMarker(location); 
     } 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 
    private void drawMarker(Location location){ 
     map.clear(); 

// convert the location object to a LatLng object that can be used by the map API 
     LatLng currentPosition = new LatLng(location.getLatitude(), location.getLongitude()); 

// zoom to the current location 
     map.animateCamera(CameraUpdateFactory.newLatLngZoom(currentPosition,16)); 


// add a marker to the map indicating our current position 
     map.addMarker(new MarkerOptions() 
       .position(currentPosition).title("Marker") 
         // .icon(BitmapDescriptorFactory.fromResource(R.drawable.blue_point)) 
       .snippet("Lat:" + location.getLatitude() + "Lng:" + location.getLongitude())); 
    } 
} 

私は私が日を過ごす私が欠けていた場合は、pls help.Your種類のサポートがappreciated.Sinceになります知っているdidnot frmこれを克服しますが、使用しません。

答えて

0
@Override 
    public void onLocationChanged(Location location) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 

     LatLng coordinate = new LatLng(lat, lng); 

     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(coordinate, 10); 

     map.animateCamera(cameraUpdate); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission 
       (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      locationManager.removeUpdates(this); 
      return; 
     } 

    } 

このコードは私のために機能します。また、現在の場所をフォーカスします。

関連する問題