我正在尝试创建一个地图活动,显示用户当前位置,可以缩放到街道级别,但我的应用程序无法正常工作,您认为我需要添加的代码是什么?
public class Map_Activity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener{
private GoogleMap myMap;
private LocationClient myLocationClient;
private static final LocationRequest REQUEST = LocationRequest.create()
.setInterval(5000)
.setFastestInterval(16)
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getMapReference();
}
@Override
protected void onResume(){
super.onResume();
getMapReference();
wakeUpLocationClient();
myLocationClient.connect();
}
@Override
public void onPause(){
super.onPause();
if (myLocationClient != null){
myLocationClient.disconnect();
}
}
private void gotoMyLocation(double lat, double lng) {
changeCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().target(new LatLng(lat, lng))
.zoom(15.5f)
.bearing(0)
.tilt(25)
.build()
), new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
}
@Override
public void onCancel() {
}
});
}
private void wakeUpLocationClient(){
if(myLocationClient == null){
myLocationClient = new LocationClient(getApplicationContext(),this,this);
}
}
private void getMapReference(){
if(myMap==null){
myMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
if (myMap != null){
myMap.setMyLocationEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_map, menu);
return true;
}
@Override
public void onConnected(Bundle bundle){
myLocationClient.requestLocationUpdates( REQUEST, this);
}
@Override
public void onDisconnected(){
}
@Override
public void onLocationChanged(Location location){
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult){
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void changeCamera(CameraUpdate update, GoogleMap.CancelableCallback callback) {
myMap.moveCamera(update);
}
}
该应用正在运行,但地图未显示且未显示我当前位置:(我希望有人可以帮助我
答案 0 :(得分:0)
以下是使用GoogleMaps的一步一步:
A.配置您的项目以使用GoogleMaps(如果配置不正确,您的地图可能不会显示):
compile 'com.google.android.gms:play-services:8.1.0'
" Android.Manifest"添加权限,GL和API密钥(确保放置您的APIKey):
< uses-permission android:name="android.permission.WAKE_LOCK" />
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
< uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
< uses-permission android:name="android.permission.INTERNET" />
< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
< uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<!-- Google Maps Fragment API Key Data -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR API KEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
B中。代码:
使用OnMapReady:
添加&#34; OnMapReadyCallback&#34;到你的活动课:
public class Map_Activity extends FragmentActivity implements GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener,
OnMapReadyCallback
{
覆盖onMapReady以启用位置并引用GoogleMap:
@Override
public void onMapReady(GoogleMap map) {
map.setMyLocationEnabled(true);
googleMap = map;
}
在Activity-OnCrate中调用getMapAsync:
FragmentManager fm = getSupportFragmentManager();
SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
℃。删除/替换已弃用的代码:
-LocationClient
-replace "GooglePlayServicesClient" with GoogleApiClient