Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map Marker OnClick event

I am using Googe Map API V2 and android to make an android application. Here is the code where I put my marker:

private void addBusStopMarker(){
    final Intent intent1;
    map.addMarker(new MarkerOptions().position(new LatLng(1.28213,103.81721)).title("10009 - Bt Merah Ctrl").icon(BitmapDescriptorFactory.fromResource(R.drawable.busstopicon)).snippet("Average Commuters: 9,940"));
    map.addMarker(new MarkerOptions().position(new LatLng(1.28294,103.82166)).title("10089 - Jln Bt Merah - B08").icon(BitmapDescriptorFactory.fromResource(R.drawable.busstopicon)).snippet("Average Commuters: 2,050"));
    map.setOnMarkerClickListener(new OnMarkerClickListener()
    {

        @Override
        public boolean onMarkerClick(Marker arg0) {
             if(arg0.getTitle().equals("10009 - Bt Merah Ctrl"))
                 intent1 = new Intent(context, PopulationCharts.class);
                 startActivity(intent1);       
            return true;
        }
    });    
}

Basically what I am trying to do is when the marker is selected, it will shows the info window which is from the code above. Then when I select the snippet, it will execute the onMarkerClick event.

However, from the code above, it shows me an error message: The final local variable intent1 cannot be assigned, since it is defined in an enclosing type. Also, I not sure how to set marker snipper onClick event . Thanks in advance.

Edited portion

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
           Intent intent = new Intent(context ,PopulationCharts.class);
           startActivity(intent);


        }
    });

Error Message

07-29 12:24:12.774: E/AndroidRuntime(15704): FATAL EXCEPTION: main
07-29 12:24:12.774: E/AndroidRuntime(15704): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.googlemap/com.example.googlemap.PopulationCharts}: java.lang.NullPointerException
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.os.Looper.loop(Looper.java:137)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.ActivityThread.main(ActivityThread.java:4512)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at java.lang.reflect.Method.invokeNative(Native Method)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at java.lang.reflect.Method.invoke(Method.java:511)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at dalvik.system.NativeStart.main(Native Method)
07-29 12:24:12.774: E/AndroidRuntime(15704): Caused by: java.lang.NullPointerException
07-29 12:24:12.774: E/AndroidRuntime(15704):    at com.example.googlemap.PopulationCharts.<init>(PopulationCharts.java:28)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at java.lang.Class.newInstanceImpl(Native Method)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at java.lang.Class.newInstance(Class.java:1319)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
07-29 12:24:12.774: E/AndroidRuntime(15704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
07-29 12:24:12.774: E/AndroidRuntime(15704):    ... 11 more
07-29 12:24:20.563: I/Process(15704): Sending signal. PID: 15704 SIG: 9

Population class

package com.example.googlemap;

public class PopulationCharts extends Activity{

    Intent intent = getIntent();
    String markerTitle= intent.getExtras().getString("markertitle");

    //Pie chart 
    private GraphicalView chartPop;
    private String[] ageGroupPop;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.population_charts);
        openChartAgeGroupPop();
        openChartPeakPop();
    }

    private void openChartAgeGroupPop(){     
        //codes to generate pie chart
    }

    //Line chart
    private View lineChart;
    private String[] time = new String[] {
            "0700", "0800" , "0900", "1000", "1100", "1200", "1300", "1400", "1500", "1600", "1700", "1800"};

    public void openChartPeakPop(){
        //code to generate multiple line chart
    }
}

1 Answers

You can use the OnInfoWindowClickListener so when you click the marker it will show first the Infowindow and when the window is click it will then start the activity

sample:

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {
       Intent intent1 = new Intent(context, PopulationCharts.class);
       String title = marker.getTitle();
       intent1.putExtra("markertitle", title);
       startActivity(intent1);  
    }
});

to get the value in PopulationCharts activity class

Intent intent = getIntent();
String markerTitle= intent.getExtras().getString("markertitle");
like image 128
Rod_Algonquin Avatar answered Dec 02 '25 15:12

Rod_Algonquin