Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add marker on google maps android?

Tags:

I am a beginner in Android programming. I already looked at similar questions and answers but I still can't figure out why this doesn't work. When I try this on the emulator and click a location, no marker appears. This is my code:

Edit: I get a marker when I click now with the following code. It only gives me a runtime exception (NullPointer exception) when I click the map the second time:

public class MapViewFragment extends Fragment {      MapView mMapView;     private GoogleMap googleMap;      @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View rootView = inflater.inflate(R.layout.fragment_map_view, container, false);         mMapView = (MapView) rootView.findViewById(R.id.mapView);         mMapView.onCreate(savedInstanceState);         mMapView.onResume();          Context context = getActivity().getApplicationContext();          final LocationsDB locationsDB = new LocationsDB(context);          try {             MapsInitializer.initialize(getActivity().getApplicationContext());         } catch (Exception e) {             e.printStackTrace();         }          mMapView.getMapAsync(new OnMapReadyCallback() {             @Override             public void onMapReady(GoogleMap mMap) {                 googleMap = mMap;                 googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {                     public void onMapClick(LatLng point) {                         // Drawing marker on the map                         MarkerOptions markerOptions = new MarkerOptions();                         markerOptions.position(point);                         markerOptions.title(point.latitude + " : " + point.longitude);                         googleMap.clear();                         googleMap.animateCamera(CameraUpdateFactory.newLatLng(point));                         googleMap.addMarker(markerOptions);                         // Create location object                         Location location = new Location(point.latitude, point.longitude);                         // add location to SQLite database                         locationsDB.insert(location);                     }                 });             }         });         return rootView;     } } 

Log messages:

7-20 07:33:00.076 5359-5359/? W/RcsService: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.rcs.service.service.a.a()' on a null object reference      at com.google.android.rcs.service.e.b(SourceFile:43)      at com.google.android.rcs.service.service.JibeService.onDestroy(SourceFile:162)      at android.app.ActivityThread.handleStopService(ActivityThread.java:3569)      at android.app.ActivityThread.-wrap26(Unknown Source:0)      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1703)      at android.os.Handler.dispatchMessage(Handler.java:105)      at android.os.Looper.loop(Looper.java:164)      at android.app.ActivityThread.main(ActivityThread.java:6540)      at java.lang.reflect.Method.invoke(Native Method)      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)   07-20 07:33:00.079 5359-5359/? E/ActivityThread: Service com.google.android.rcs.service.service.JibeService has leaked IntentReceiver com.google.android.rcs.service.provisioning.RcsReconfigurationSmsReceiver@741339 that was originally registered here. Are you missing a call to unregisterReceiver()?    android.app.IntentReceiverLeaked: Service com.google.android.rcs.service.service.JibeService has leaked IntentReceiver com.google.android.rcs.service.provisioning.RcsReconfigurationSmsReceiver@741339 that was originally registered here. Are you missing a call to unregisterReceiver()?      at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1310)      at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1091)      at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1397)      at android.app.ContextImpl.registerReceiver(ContextImpl.java:1370)      at android.app.ContextImpl.registerReceiver(ContextImpl.java:1358)      at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:603)      at com.google.android.rcs.service.provisioning.RcsSmsReceiver.a(SourceFile:12)      at com.google.android.rcs.service.h.g(SourceFile:230)      at com.google.android.rcs.service.h.<init>(SourceFile:212)      at com.google.android.rcs.service.service.a.<init>(SourceFile:13)      at com.google.android.rcs.service.e.a(SourceFile:32)      at com.google.android.rcs.service.service.JibeService.d(SourceFile:145)      at com.google.android.rcs.service.service.JibeService.onCreate(SourceFile:91)      at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404)      at android.app.ActivityThread.-wrap4(Unknown Source:0)      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683)      at android.os.Handler.dispatchMessage(Handler.java:105)      at android.os.Looper.loop(Looper.java:164)      at android.app.ActivityThread.main(ActivityThread.java:6540)      at java.lang.reflect.Method.invoke(Native Method)      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
like image 535
no-name Avatar asked Jul 20 '17 07:07

no-name


People also ask

How do I set a marker icon?

You can set a custom icon within the marker's constructor, or by calling setIcon() on the marker. See more about customizing the marker image. Broadly speaking, markers are a type of overlay. For information on other types of overlay, see Drawing on the map.


2 Answers

Look here in Google Maps example https://developers.google.com/maps/documentation/android-api/marker

If you want to add marker when clicking you can also look here: http://wptrafficanalyzer.in/blog/adding-marker-on-touched-location-of-google-maps-using-android-api-v2-with-supportmapfragment/

The concept is to do like the folowing code:

// Setting a click event handler for the map     googleMap.setOnMapClickListener(new OnMapClickListener() {          @Override         public void onMapClick(LatLng latLng) {              // Creating a marker             MarkerOptions markerOptions = new MarkerOptions();              // Setting the position for the marker             markerOptions.position(latLng);              // Setting the title for the marker.             // This will be displayed on taping the marker             markerOptions.title(latLng.latitude + " : " + latLng.longitude);              // Clears the previously touched position             googleMap.clear();              // Animating to the touched position             googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));              // Placing a marker on the touched position             googleMap.addMarker(markerOptions);         }     }); 
like image 191
Sagi Mymon Avatar answered Sep 23 '22 17:09

Sagi Mymon


//Show Marker on a Location googleMap.addMarker(new MarkerOptions().position(TIMES_SQUARE));  //Change Default Color of Marker  googleMap.addMarker(new MarkerOptions()             .position(BROOKLYN_BRIDGE)             .title("First Pit Stop")             .icon(BitmapDescriptorFactory             .defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));  //Replace Default Marker Icon with Custom Image  googleMap.addMarker(new MarkerOptions()             .position(WALL_STREET)             .title("Wrong Turn!")             .icon(BitmapDescriptorFactory             .fromResource(R.drawable.my_flag))); 
like image 23
Mukesh Kumar Swami Avatar answered Sep 23 '22 17:09

Mukesh Kumar Swami