Reputation: 805
Adding Google Maps to a project and i have just created a practice project first to test it. I want to be able to click the markers that i add however i get an error saying method onMarkerClick is never used.
My code is:
package uk.co.ryanmoss.cameraproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ViewSwitcher;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity {
private ViewSwitcher switcher;
private static final int REFRESH_SCREEN = 1;
private GoogleMap gMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
Button button1 = (Button) findViewById(R.id.button1);
initialiseMap();
}
public void initialiseMap(){
if (gMap == null){
gMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
gMap.setOnMarkerClickListener((GoogleMap.OnMarkerClickListener) this);
}
}
public boolean onMarkerClick(Marker marker){
switcher.showNext();
return false;
}
public void changeScreen(View view){
switcher.showNext();
}
public void changeScreenBack(View view){
switcher.showPrevious();
markerMap(gMap);
}
public void markerMap(GoogleMap map){
map.addMarker(new MarkerOptions()
.position(new LatLng(53.7832138, -2.6556234))
.title("Hello world"))
.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.arrowdown));
}
@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_main, menu);
return true;
}
@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);
}
}
Upvotes: 0
Views: 2045
Reputation: 1789
I don't know what the problem, but if you implements the interface method on your class of GoogleMap.OnMarkerClickListener not works, but inside the object work's fine! like @kelvincer says before
Upvotes: 0
Reputation: 6138
As Byron says you must do this:
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
Upvotes: 3
Reputation: 1313
Your class is not of type GoogleMap.OnMarkerClickListener as you have indicated here with the this parameter:
gMap.setOnMarkerClickListener((GoogleMap.OnMarkerClickListener) this);
The method public boolean onMarkerClick(Marker marker)
is a method of the GoogleMap.OnMarkerClickListener interface but your class is an Activity. You need to create an anonymous class that contains this method.
Upvotes: 0