How can I add a Marker in google map? - android

I want to add one Marker in Google map. I successfully loaded map in my app but when I tried to add a Marker, the app got crashed. I don't know why. Please some one help me!
My code:
public class BasicMapActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_demo);
setUpMap();
}
#Override
protected void onResume() {
super.onResume();
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

Create a method setUpMapIfNeeded() and called on onResume() and onCreate()
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
First you need to obtain the map from the SupportMapFragment and then you add Marker into map using
mMap.addMarker(new MarkerOptions().position(new LatLng(Your_lat, Your_long)).title("Marker"));

Try This
private void setUpMap() {
Marker pos_Marker = googleMap.addMarker(new MarkerOptions().position(starting).icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_laumcher)).title("Starting Location").draggable(false));
pos_Marker.showInfoWindow();
}

Related

Marker is not working(not exact location is displaying), only world Map is visible in Frgament

CustomMapFragment
public class CustomMapFragment extends SupportMapFragment {
public static final LatLng NurissLife = new LatLng(28.602012, 77.098750);
private SupportMapFragment supportMapFragment;
#Override
public void onActivityCreated(Bundle bundle) {
// FragmentManager fm=getChildFragmentManager();
supportMapFragment=SupportMapFragment.newInstance();
//supportMapFragment=(SupportMapFragment)getChildFragmentManager
//().findFragmentById(R.id.map_container);
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
super.onActivityCreated(bundle);
}
}
Fragment xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map_content"
tools:context="com.demo.stuartabhi.nurisslife.Fragment.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:layout_width="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_height="match_parent"
android:id="#+id/map_container">
</FrameLayout>
</FrameLayout>'
Marker is not working(exact location is not displaying), only world Map
is visible in Fragement
OnMapReady is not working either showing
no error in log, location code being described in OnMapReady method.
Please help regarding this, how to access the described location LatLng NurissLife inside SupportMapFragment.
Thanks in advance.
If you extend SupportMapFragment, there is no need to inflate any xml layout. Just call this.getMapAsync() in order to get a reference to the GoogleMap:
public class CustomMapFragment extends SupportMapFragment implements
OnMapReadyCallback {
private GoogleMap mMap;
public CustomMapFragment() {
}
#Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
getMapAsync(this);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d("MyMap", "onMapReady");
mMap = googleMap;
mMap.addMarker(new MarkerOptions().position(NurissLife).title("Nuriss LifeCare"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(NurissLife, 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
}
Try this:
Create a method getCustomMapFragment() in your Activity class like this:
private SupportMapFragment getCustomMapFragment(){
SupportMapFragment supportMapFragment;
supportMapFragment= SupportMapFragment.newInstance();
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.addMarker(new MarkerOptions().position
(NurissLife).title("Nuriss LifeCare"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(NurissLife, 15));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000,
null);
}
});
return supportMapFragment;
}
And when you perform your FragmentTransaction, try this:
...
transaction=getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_container,getCustomMapFragment()).commit();
...
Also, with this implementation, the class="com.google.android.gms.maps.SupportMapFragment" line in your XML might be unnecessary.

Clusters don't show when i start the app on googlemapsv2

I have a map and i have a database with json query. I'm trying to create a map where you have markers(title etc.). Because there are so many of them i thought of clustering, so i implemented them. But the problem is that it doesn't work as i want it to. When the app starts, it shows the map, zooms in on your location, but doesn't shows you any markers or clusters. But at the moment you zoom out, the clusters shows, with markers etc. Any ideas what is wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
if (getSharedPreferencesRadius() == null) {
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
editor.putInt("radius", 2000);
editor.commit();
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setUpMapIfNeeded();
}
#Override
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;
setUpMap();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Log.d("MAP", " "+mMap);
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
setMyLocationLatLng();
kompasEnabled();
radiusEnable(latLng);
ZoomIN(latLng);
mClusterManager = new ClusterManager<Marker>(this, mMap);
mClusterManager.clearItems();
mMap.setOnCameraChangeListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
mClusterManager.cluster();
jsonRequest(latitude, longitude);
}
protected GoogleMap getMap() {
setUpMapIfNeeded();
return mMap;
}
And this is activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"></include>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/clayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context=".MainActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
If you need any more info, please ask.
UPDATE 1:
I found out that without mMap.setOnCameraChangeListener(mClusterManager);
It doesn't work(won't even show them). Any ideas?
I found the solution, after hours and hours of looking at my code, and searching the internet i found, that you need when you create all the Items, you need to call:
mClusterManager.cluster();
That is it.

GoogleMaps.oncameraChangedListener is never called

I have an Activity (which inflates a map fragment) which implements the GoogleMap.onCameraChangedListener, and I have overriden the onCameraChange method.
The problem is that whenever I move across the map, i.e. the camera position changes, the onCameraChange method is never called.
What could be causing this?
Try this :
public class MapActivity extends AppCompatActivity implements
GoogleMap.OnCameraChangeListener{
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.googlemap_layout);
setUpMapIfNeeded();
}
#Override
public void onCameraChange(CameraPosition cameraPosition) {
double latitude = cameraPosition.target.latitude;
double longitude = cameraPosition.target.longitude;
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
// Try to obtain the map from the SupportMapFragment.
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(false);
googleMap.setOnCameraChangeListener(this);
}
}
}
}
Layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

Mylocation button dont show up

I'm using Android Studio and using a map activity my map shows up fine, but the Mylocation button in the activity doesn't show. My google play services are installed. What am I doing wrong?
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
if (mMap != null) {
mMap.setMyLocationEnabled(true);
}
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
You have to add this line in your code, replace your old code for this one :
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
if (mMap != null) {
//edit this
}
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setMyLocationEnabled(true);//here's the button
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
Also you have to add this in your manifest
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
If my answer didn't help you, you can follow this tutorial and do what it says step by step :)
Check this for your setUpMapIfNeeded() method:
Android Google Maps setMyLocationEnabled(true)
To enable the location on your Google Maps if the Google Maps is visible on the activity/fragment.
Create an instance of the Google Map:
GoogleMap googleMap;
Inside your main activity that implements LocationListener
Add this line in your code to see the My Location Button:
googleMap.setMyLocationEnabled(true);
Don't forget to add the permissions in the manifest file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
For more details please refer to this tutorial.

Starting a map with current location

public class MainActivity extends FragmentActivity{
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
//mMap.setMyLocationEnabled(true);
}
}
This is my Activity.
I want to start my app with my current location. I looked for some codes but none have the answer that I want. Can someone tell me what I should add?
Did you try:
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
map.animateCamera(cameraUpdate);
Please use Android's LocationManager to get Lat/Lng with respect to your current services. Please refer to this answer that explains the usage of LocationManager.
After getting a Location object from the LocationManager, use the lat/lng to position the map like this.

Categories

Resources