我在google地图上遇到问题,这是一个片段。即使我有
的代码map.setMyLocationEnabled(true);
map.getUiSettings().setAllGesturesEnabled(true);
LatLng pos= new LatLng(1.464,110.426);
map.addMarker(new MarkerOptions().position(pos));
我的地图从未在代码中向我显示任何内容。蓝色指示符未显示为我当前的位置和标记无法看到。
它适用于计步器,其中计步器细节(步数,距离,速度,卡路里)显示在顶部,而地图显示在计步器细节下方。以下是我的代码:
fragment_pedo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/screen_background"
android:orientation="vertical"
android:padding="@dimen/margin" >
//more codes, XML for pedo
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MapFragment.java
public class MapFragment extends Fragment implements
LocationListener, GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private static View v;
private LocationRequest lr;
private LocationClient lc;
private LatLng point;
GoogleMap map;
float mLat, mLng;
Marker marker;
public MapFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("location", "onCreateView");
v = inflater.inflate(R.layout.fragment_pedo, container, false);
SupportMapFragment mapFragment = ((SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map));
map = mapFragment.getMap();
map.getUiSettings().setAllGesturesEnabled(true);
map.getUiSettings().setRotateGesturesEnabled(true);
map.getUiSettings().setMyLocationButtonEnabled(true);
map.setMyLocationEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
LatLng pos= new LatLng(1.464,110.426);
map.addMarker(new MarkerOptions().position(pos));
MapsInitializer.initialize(this.getActivity());
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("location", "onCreate");
lr = LocationRequest.create();
// lr.setInterval(1000);
lr.setNumUpdates(2);
lc = new LocationClient(this.getActivity().getApplicationContext(),
this, this);
lc.connect();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
Log.d("location", "onConnected");
Toast.makeText(getActivity(), "You may long tap to set your location.", Toast.LENGTH_LONG).show();
lc.requestLocationUpdates(lr, this);
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
Log.d("location", "onDisconnected");
Toast.makeText(getActivity(), "Disconnected. Please re-connect.",
Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location arg0) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(arg0.getLatitude(), arg0.getLongitude()))
.zoom(15).build();
Log.d("location", "myLatLng=" + cameraPosition);
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
point = new LatLng(arg0.getLatitude(), arg0.getLongitude());
mLat = (float) point.latitude;
Log.d("tag", "mLat=" + mLat);
mLng = (float) point.longitude;
Log.d("location", "mLng=" + mLng);
myMarker();
Log.d("location", "do you run?" + "do you run?");
}
// add a new marker
public Marker myMarker() {
marker = map.addMarker(new MarkerOptions().position(point).title(
point.toString()));
return marker;
}
}
为什么我在地图上的当前位置和其他活动无法显示?请帮忙。百万提前感谢。
答案 0 :(得分:3)
检查您是否正确添加了AndroidManifest中的权限。
//显示当前位置所必需的
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//也需要互联网权限。
<uses-permission android:name="android.permission.INTERNET" />
要显示标记,请记住缩放并为相机设置动画,如下所示:
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("title")
.snippet("some text in ballon");
CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(18).build();//18 was a number it was fine to my situation.
Marker markerFinal = googleMap.addMarker(markerOptions);
markerFinal.showInfoWindow();//the marker comes with balloon already open.
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
此致
Uilton Santos。