我有下一个问题:我希望获得当前位置,之后我想在它周围绘制一些标记。我使用LocationListener
来获取当前位置,但是当我运行我的应用程序时,我看到了空地图,几秒钟后我得到当前位置并绘制标记。我想下一步:我想在获得当前位置后加载地图。我不想看到没有标记的地图。我的代码是:
public class WhereAmIFragment extends BaseContainerFragment implements LocationListener {
private SupportMapFragment mMapFragment;
private LocationManager locationManager;
public static WhereAmIFragment newInstance() {
return new WhereAmIFragment();
}
private GoogleMap.OnMapClickListener mOnMapClickListener = new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_where_am_i, container, false);
mMapFragment = SupportMapFragment.newInstance();
getChildFragmentManager().beginTransaction().replace(R.id.map_container, mMapFragment).commit();
return root;
}
@Override
public void onStart() {
super.onStart();
mMapFragment.getMap().setMyLocationEnabled(true);
mMapFragment.getMap().setOnMapClickListener(mOnMapClickListener);
locationManager = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(WhereAmIFragment.this);
locationManager = null;
drawStops(new LatLng(location.getLatitude(), location.getLongitude()));
}
private void drawStops(LatLng latLng) {
DefaultCallback<ArrayList<Stop>> callback = new DefaultCallback<ArrayList<Stop>>(getActivity()) {
@Override
public void success(ArrayList<Stop> stops, Response response) {
Log.d("myLogs", "array list size= " + stops.size());
for (int i = 0; i < stops.size(); i++) {
Log.d("myLogs", i + ". " + stops.get(i).getLng());
drawMarkers(stops);
}
}
};
mMapFragment.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.0f));
RequestDataBuilder builder = new RequestDataBuilder(getActivity()).prepareWhereAmIData(
latLng, 500);
EwayApi api = RestAdaptersProvider.getApi(new StopsConverter());
api.sendRequest(builder, callback);
}
private void drawMarkers(ArrayList<Stop> arrayList) {
for (int i = 0; i < arrayList.size(); i++) {
mMapFragment.getMap().addMarker(new MarkerOptions()
.position(arrayList.get(i).getLng())
.title(arrayList.get(i).getTitle()));
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
答案 0 :(得分:0)
地图可以是init as,
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
map = fm.getMap();
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
您可以添加标记,例如
LatLng toPosition = new LatLng(Promolat, Promolon);
map.addMarker(new MarkerOptions()
.position(toPosition)
.title(promotions.getPromotionHeading().toString())
.snippet(merchants.getMerchantAddress().toString())
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(toPosition, 15));