Add Marking To Google Map (Google Maps Android Api V2)

Posted on


To add together mark to Google Map of Google Maps Android API v2:

                        LatLng targetLatLng = novel LatLng(lat, lon);                         MarkerOptions markerOptions =                                 novel MarkerOptions().position(targetLatLng).title(strTitle);                         mMap.addMarker(markerOptions); 

Modify from lastly illustration “Set map type for Google Maps Activity using Google Maps Android API v2” to add together characteristic to add together markers on Google Maps Activity.

Edit values/strings.xml to add together string resources of “menu_addmarkers”

<resources>     <string name="app_name">AndroidStudioMapApp</string>     <string name="title_activity_maps">Map</string>     <string name="menu_legalnotices">Legal Notices</string>     <string name="menu_about">About</string>     <string name="menu_addmarkers">Add Markers</string> </resources>  

Edit menu/activity_main.xml, to add together carte pick of Add Markers:

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto">     <item         android:id="@+id/menu_about"         android:orderInCategory="100"         app:showAsAction="ifRoom"         android:title="Add Marker to Google Map (Google Maps Android API v2)"/>     <item         android:id="@+id/menu_addmarkers"         android:orderInCategory="100"         app:showAsAction="ifRoom"         android:title="Add Marker to Google Map (Google Maps Android API v2)"/>     <item         android:id="@+id/maptype"         android:orderInCategory="100"         app:showAsAction="never"         android:title="Add Marker to Google Map (Google Maps Android API v2)">         <menu >             <group android:id="@+id/groupmaptype"                 android:checkableBehavior="single">                 <item android:id="@+id/maptypeNORMAL"                     android:title="Add Marker to Google Map (Google Maps Android API v2)" />                 <item android:id="@+id/maptypeSATELLITE"                     android:title="Add Marker to Google Map (Google Maps Android API v2)" />                 <item android:id="@+id/maptypeTERRAIN"                     android:title="Add Marker to Google Map (Google Maps Android API v2)" />                 <item android:id="@+id/maptypeHYBRID"                     android:title="Add Marker to Google Map (Google Maps Android API v2)" />                 <item android:id="@+id/maptypeNONE"                     android:title="Add Marker to Google Map (Google Maps Android API v2)" />              </group>         </menu>      </item>     <item         android:id="@+id/menu_legalnotices"         android:orderInCategory="100"         app:showAsAction="never"         android:title="Add Marker to Google Map (Google Maps Android API v2)"/>   </menu> 

Modify MapsActivity.java

package com.blogspot.android_er.androidstudiomapapp;  import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.InputType; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast;  import com.google.android.gms.common.GoogleApiAvailability; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions;  world shape MapsActivity extends AppCompatActivity implements OnMapReadyCallback {      soul GoogleMap mMap;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_maps);         // Obtain the SupportMapFragment as well as larn notified when the map is create to live used.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()                 .findFragmentById(R.id.map);         mapFragment.getMapAsync(this);      }      /**      * Manipulates the map i time available.      * This callback is triggered when the map is create to live used.      * This is where nosotros tin add together markers or lines, add together listeners or motion the camera. In this case,      * nosotros only add together a mark nigh Sydney, Australia.      * If Google Play services is non installed on the device, the user volition live prompted to install      * it within the SupportMapFragment. This method volition entirely live triggered i time the user has      * installed Google Play services as well as returned to the app.      */     @Override     world void onMapReady(GoogleMap googleMap) {         mMap = googleMap;     }      @Override     world boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         furnish super.onCreateOptionsMenu(menu);     }      @Override     world boolean onOptionsItemSelected(MenuItem item) {         switch (item.getItemId()) {             instance R.id.menu_addmarkers:                 addMarker();                 furnish true;             instance R.id.maptypeHYBRID:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);                     furnish true;                 }             instance R.id.maptypeNONE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NONE);                     furnish true;                 }             instance R.id.maptypeNORMAL:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);                     furnish true;                 }             instance R.id.maptypeSATELLITE:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);                     furnish true;                 }             instance R.id.maptypeTERRAIN:                 if(mMap != null){                     mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);                     furnish true;                 }             instance R.id.menu_legalnotices:                 String LicenseInfo = GoogleApiAvailability                         .getInstance()                         .getOpenSourceSoftwareLicenseInfo(MapsActivity.this);                 AlertDialog.Builder LicenseDialog =                         novel AlertDialog.Builder(MapsActivity.this);                 LicenseDialog.setTitle("Legal Notices");                 LicenseDialog.setMessage(LicenseInfo);                 LicenseDialog.show();                 furnish true;             instance R.id.menu_about:                 AlertDialog.Builder aboutDialogBuilder =                         novel AlertDialog.Builder(MapsActivity.this);                 aboutDialogBuilder.setTitle("About Me")                         .setMessage("https://rootandroid.org/");                  aboutDialogBuilder.setPositiveButton("visit",                         novel DialogInterface.OnClickListener() {                     @Override                     world void onClick(DialogInterface dialog, int which) {                         String url = "https://rootandroid.org/";                         Intent i = novel Intent(Intent.ACTION_VIEW);                         i.setData(Uri.parse(url));                         startActivity(i);                     }                 });                  aboutDialogBuilder.setNegativeButton("Dismiss",                         novel DialogInterface.OnClickListener() {                     @Override                     world void onClick(DialogInterface dialog, int which) {                         dialog.cancel();                     }                 });                  AlertDialog aboutDialog = aboutDialogBuilder.create();                 aboutDialog.show();                  furnish true;         }         furnish super.onOptionsItemSelected(item);     }      soul void addMarker(){         if(mMap != null){              //create custom LinearLayout programmatically             LinearLayout layout = novel LinearLayout(MapsActivity.this);             layout.setLayoutParams(new LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT,                     LinearLayout.LayoutParams.WRAP_CONTENT));             layout.setOrientation(LinearLayout.VERTICAL);              concluding EditText titleField = novel EditText(MapsActivity.this);             titleField.setHint("Title");              concluding EditText latField = novel EditText(MapsActivity.this);             latField.setHint("Latitude");             latField.setInputType(InputType.TYPE_CLASS_NUMBER                     | InputType.TYPE_NUMBER_FLAG_DECIMAL                     | InputType.TYPE_NUMBER_FLAG_SIGNED);              concluding EditText lonField = novel EditText(MapsActivity.this);             lonField.setHint("Longitude");             lonField.setInputType(InputType.TYPE_CLASS_NUMBER                     | InputType.TYPE_NUMBER_FLAG_DECIMAL                     | InputType.TYPE_NUMBER_FLAG_SIGNED);              layout.addView(titleField);             layout.addView(latField);             layout.addView(lonField);              AlertDialog.Builder builder = novel AlertDialog.Builder(this);             builder.setTitle("Add Marker");             builder.setView(layout);             AlertDialog alertDialog = builder.create();              builder.setPositiveButton("OK", novel DialogInterface.OnClickListener() {                 @Override                 world void onClick(DialogInterface dialog, int which) {                     boolean parsable = true;                     Double lat = null, lon = null;                      String strLat = latField.getText().toString();                     String strLon = lonField.getText().toString();                     String strTitle = titleField.getText().toString();                      try{                         lat = Double.parseDouble(strLat);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Latitude does non incorporate a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      try{                         lon = Double.parseDouble(strLon);                     }catch (NumberFormatException ex){                         parsable = false;                         Toast.makeText(MapsActivity.this,                                 "Longitude does non incorporate a parsable double",                                 Toast.LENGTH_LONG).show();                     }                      if(parsable){                         LatLng targetLatLng = novel LatLng(lat, lon);                         MarkerOptions markerOptions =                                 novel MarkerOptions().position(targetLatLng).title(strTitle);                         mMap.addMarker(markerOptions);                         mMap.moveCamera(CameraUpdateFactory.newLatLng(targetLatLng));                     }                 }             });             builder.setNegativeButton("Cancel", null);              builder.show();         }else{             Toast.makeText(MapsActivity.this, "Map non ready", Toast.LENGTH_LONG).show();         }     } }  

                        LatLng targetLatLng  Add Marker to Google Map (Google Maps Android API v2)Download the files (Android Studio Format) .

Next:
– initialize map inward xml


Step-by-step of Android Google Maps Activity using Google Maps Android API v2, on Android Studio