2016-05-24 59 views
0

我想实现谷歌地图API。我希望在地图上看到自己的位置,并且希望在我移动时有所改变。问题是没有错误,但它不显示我在地图上的位置标记。这是我的onCreate()在java文件谷歌地图Android API和当前位置

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapPane.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map_pane); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    // 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 

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

梅索德这是我觉得我失去了一些东西

public void onConnected(@Nullable Bundle bundle) { 
    Log.i(TAG, "Location services connected."); 

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null) { 
     Log.i(TAG, "loc exists"); 
     if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
       || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
     }//if 
    }//if 
    else { 
     handleNewLocation(location); 
    }//else 
}//onConnected 

,因为我从来没有在第二if语句的方法。我想我首先需要用户的许可?在我的设备上手动打开gps也不起作用。我用下面的方法在地图上显示标记

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)); 

}//handleNewLocation 

我用了更多的方法,但我认为那里没有缺陷。在AndroidManifest.xml我包括

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
+0

什么是地点的价值? –

+0

它是空的,因为我可以看到“Log.i(TAG,”loc存在“);”在我的Android监视器 – Qas

+0

中,您是否像本教程一样添加了onStart()和onStop()方法https://developer.android.com/training/location/retrieve-current.html? –

回答

0

您可以使用,而不是谷歌的API客户端

   LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
    final Geocoder gc = new Geocoder(this, Locale.getDefault()); 

    LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      //here you can get current position location.getLatitude(),location.getLongitude(); 

     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
0

看来你的代码不给handlelocation()方法提供任何位置的LocationManager。

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public static final String TAG = MapPane.class.getSimpleName(); 
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 
LocationListener li; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map_pane); 
    // Obtain the SupportMapFragment and get notified when the map is reato be used. 
    SupportMapFragment mapFragment = (SupportMapFragment)   getSupportFragmentManager() 
     .findFragmentById(R.id.map); 
mapFragment.getMapAsync(this); 

li=new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      handleNewLocation(location); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 
    }; 


    getHandledCurrentLocation(); 
}//onCr 

private void getHandledCurrentLocation() { 
    String locationProvider = LocationManager.NETWORK_PROVIDER; 

    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. 
     return; 
    } 
    Log.e("LOCATION_DEBUG","LOCATION REQUESTED--- "); 
    LCM.requestLocationUpdates(locationProvider, 10 * 1000, 0,li); 

} 
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)); 

}//handleNewLocation 

此代码应该工作。

+0

如果它显示任何错误,通知我.. –

0

我可以看到你的代码有几个问题。我不认为你正在处理FusedLocationApi提供的位置更新。即使您的问题中未提供部分代码,我相信您正在实施LocationListener。在这种情况下,您在您的活动中实施了一种方法'onLocationChanged(Location location)'。这是您应该尝试在地图上设置新位置的方法。您的handleNewLocation方法将不会由FusedLocationApi自动调用

您可能希望为您的活动提供完整的代码,然后我们可以帮助您更好地完成。

相关问题