Reputation: 21
I have created a google map fragment in android but marker is not showing. Map is showing perfectly but not marker, And how can I set user current location as marker ?
public class GoogleMap extends Fragment implements OnMapReadyCallback {
private com.google.android.gms.maps.GoogleMap googleMap;
private static final LatLng Ahmedabad = new LatLng(23.022214, 72.542786);
public GoogleMap() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_google_map, container, false);
processMap(v);
return v;
}
@Override
public void onMapReady(com.google.android.gms.maps.GoogleMap googleMap) {
this.googleMap = googleMap;
}
public void processMap(View v) {
if (googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(Ahmedabad).title("Ahmedabad"));
// TODO: Consider calling
}
}
}
Upvotes: 0
Views: 812
Reputation: 109
You should call processMap(v) after the map ready. when you tried to call processMap onCreateView, your google map is not ready yet , your googleMap variable is null
private com.google.android.gms.maps.GoogleMap googleMap;
private static final LatLng Ahmedabad = new LatLng(23.022214, 72.542786);
public GoogleMap() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_google_map, container, false);
return v;
}
@Override
public void onMapReady(com.google.android.gms.maps.GoogleMap googleMap) {
this.googleMap = googleMap;
processMap();
}
public void processMap() {
if (googleMap != null) {
googleMap.addMarker(new MarkerOptions().position(Ahmedabad).title("Ahmedabad"));
}
}
Upvotes: 1