startActivity illegal start of type - android

As of this morning I've tried to put together enough knowledge to produce a very basic app to demonstrate a concept. The idea is to display google maps, the user presses on where they want to add a marker, then a screen pops up where they can fill out more information that is then displayed when someone taps that marker.
This is what I got from the Android Studio base.
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
private void setMapLongClick(final GoogleMap map) {
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng latLng) {
}
});
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.z
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Move the camera to Delft
LatLng delft = new LatLng(52.003569, 4.372987);
Float zoom = 15f;
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delft, zoom));
setMapLongClick(mMap);
}
}
I tried adding this
private void setMarkerClick(final GoogleMap map) {
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
Intent intent = new Intent(this, MainActivity.class);
this.startActivity(intent);
});
}
But I get and "illegal start of type" error. Am I doing this completely wrong?
Is there an easier way to add information to a marker?

This is how you should use it.
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
});

Providing this as first argument when creating the Intent might not work since it refers to the OnMarkerClickListener anonymous class and not to the packageContext. Here's my suggestion, give MapsActivity.this as reference:
map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
startActivity(intent);
return true;
}
});

You cannot call
this.startActivity(intent);
inside an anonymous class as startActivity is not a method of the anonymous class but of the enclosing activity. Therefore you should use
startActivity(intent);
Also use the proper anonymous implementation.

Related

I have a problem using onMarkerClickListeners. I am a beginner and there is an error appearing that says Missing return statement

I am getting that error in the last Override.
I really do not know what I need to return so please help me.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,GoogleMap.OnMarkerClickListener {
private GoogleMap mMap;
private Marker myMarker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng MORNAR = new LatLng(43.5201139, 16.4282208);
googleMap.setOnMarkerClickListener(this);
mMap.addMarker(new MarkerOptions().position(MORNAR).title("Stari mornar"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(MORNAR));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MORNAR, 13));
}
#Override
public boolean onMarkerClick(final Marker marker) {
String name= marker.getTitle();
if (name.equalsIgnoreCase("Stari mornar"))
{
openActivity3();
}
}
public void openActivity3(){
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
}
}
Change the maker click listener to this
#Override
public boolean onMarkerClick(final Marker marker) {
String name= marker.getTitle();
if (name.equalsIgnoreCase("Stari mornar"))
{
openActivity3();
return true;
}
return false;
}
Here the method shows that it returns boolean.
So if you click the marker and the function returns true, that means the click event is successful, and if it returns false info windows will show as usual.
In the last override method (onMapReady(Marker)) you need to return a boolean, since it is mandatory.
#Override
public boolean onMarkerClick(final Marker marker) {
String name= marker.getTitle();
if (name.equalsIgnoreCase("Stari mornar"))
{
openActivity3();
}
// return true or false
}
If you return true, that means you have clicked on the marker.
After the privacy accessor of your function ("public") you have your returning type, which is boolean in this case.
Let me know if this helped! ;)

Is it possible to have onMapClick() called instead of onMarkerClick()?

I have a google map in my app and I handle clicks on it. Everything is fine but if I click on a marker or really close to it onMarkerClick() is called, not onMapClick() and then I don't have the exact location of the place I tapped because marker.getPosition() returns the center point of the marker what's not the same.
Is it possible to disable onMarkerClick() and have onMapClick() called even if I click on a marker?
Return true for disabling marker click event
googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return true;
}
});
For getting click on maps use this listener on onMapReady
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
#Override
public void onMapClick(LatLng arg0)
{
Log.v("onMapClick", "Yea worked!");
}
});
}
Complete code
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, "Map clicked", Toast.LENGTH_SHORT).show();
}
});
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this, "Marker disabled", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
It is possible.Here is a simple solution.
First of all you should declare GoogleMap.OnMarkerClickListener and GoogleMap.OnMapClickListener in your activity or fragment like this.
public class MapActivity extends FragmentActivity implements
GoogleMap.OnMarkerClickListener,
GoogleMap.OnMapClickListener
{
//..
}
And then register that interfaces in your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState)
{
//..
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mMap.setOnMarkerClickListener(this);
mMap.setOnMapClickListener(this);
}
Finally you can implement two interfaces.You should call onMapClick() method in onMarkerClick() method to achieve that even if you click on a marker like that.Don't forget to return true in onMarkerClick() method.
#Override
public void onMapClick(LatLng latLng) {
Log.v("onMapClick", "Map Clicked..")
}
#Override
public boolean onMarkerClick(Marker marker) {
onMapClick(marker.getPosition());
return true;
}

Zoom map Get location chosen by User and pass it to activity

I have this code, that allows a user to select a location (mechanic_location) and passes it to another activity(mechanic_login). The problem is, it does not zoom. Help me enable it to zoom to the current location the user is in but give the user the ability to pick another location different from the one that he/she is in. Once the location is picked, click a button to send the latitude and longitude to the view (mechanic_login). Here is my code
public class mechanic_location extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
Button mSendLocationBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mechanic_location);
// 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);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Intent returnIntent = new Intent();
returnIntent.putExtra("picked_point",latLng);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
}
}
You can use the moveCamera() Method and defined the zoom level
float zoomLevel = 16.0f; //This goes up to 21
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
for mor information about using google map, you can check it here
source

How to use getMapAsync() instead of getMap()

I am aware that in Play Services, getMap() is depreciated, and is replaced by getMapAsync().
But I am not sure how to use getMapAsync(). I found other questions similar to this -- however, in my code, I am not sure what to implement in my getMapAsync(). I think it is supposed to be a callback method, but I am not sure.
Could someone please put the correct code in, and tell me what I am doing wrong?
public class FindParty extends AppCompatActivity {
static final LatLng Durban = new LatLng(-29.858680, 31.021840);
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_party);
try{
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
final Marker marker_Durban = googleMap.addMarker(new MarkerOptions()
.position(Durban)
.snippet("Durban, KwaZulu-Natal, South Africa")
.title("Durban"));
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(FindParty.this, Party.class);
intent.putExtra("message", "Durban");
startActivity(intent);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
your class need to implements OnMapReadyCallback
public class FindParty extends AppCompatActivity implements OnMapReadyCallback {
static final LatLng Durban = new LatLng(-29.858680, 31.021840);
private GoogleMap googleMap;
MapFragment mapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_party);
try {
if (googleMap == null) {
mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mapFragment.getMapAsync(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onMapReady(GoogleMap map) {
googleMap = map;
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setTrafficEnabled(true);
googleMap.setIndoorEnabled(true);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
final Marker marker_Durban = googleMap.addMarker(new MarkerOptions()
.position(Durban)
.snippet("Durban, KwaZulu-Natal, South Africa")
.title("Durban"));
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
if (marker.getTitle().equals("Durban")) {
Intent intent = new Intent(FindParty.this, Party.class);
intent.putExtra("message", "Durban");
startActivity(intent);
}
}
});
}
}
You are right, getMapAsync() is based on callback, please see belowe example and move your map options settings to callback method:
Example
getMapAsync() { it.isTrafficEnabled = false }
The better way I found to understand the use of getMapAsync was to create a new project in Android Studio and use the template of google maps activity instead of an empty activity.
Hope help others with the same difficulty.

How to use OnMarkerClickListener

I'm trying to make an application that allows to open new activity from a marker on the map with the method GoogleMap.OnMarkerClickListener but do not know how to use it. Does anyone help me?
Just use this:
getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
Intent i = new Intent(getActivity(), NewActivity.class);
startActivity(i);
}
});
I used this code in my custom class that extends MapFragment
I've placed it in this method:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
}
When touching Info window within a marker, if changing the current screen into another screen(class or Activity), Do this below;
private GoogleMap mMap:
.....
protected void onCreate(Bundle savedInstanceState) {
.....
private void setUpMap() {
.....
mMap.setOnInfoWindowClickListener(this);
#Override
public void onInfoWindowClick(Marker marker) {
// When touch InfoWindow on the market, display another screen.
Intent intent = new Intent(this, Another.class);
startActivity(intent);
}

Categories

Resources