Showing a custom marker on user his position - android

I am using Google Maps v2 Android MapView. The marker of the position of the user is a blue circle. I want to change this to my drawable.
Is this possible?
I know I can add markers like this but this doesn't apply to the user his own position:
this.marker = BitmapDescriptorFactory.fromResource(R.drawable.marker);
this.map.addMarker(new MarkerOptions().position(position).title(name)
.draggable(false).icon(this.marker));

Yes you can add your custom marker to show user current location....but you need to calculate the latitude and longitude your self using locationListener and show that location on map as follows:
public void setCurretnLocMarker(loc) {
LatLng mLatLng=new LatLng(loc.getLatitude(), loc.getLongitude());
getMap().animateCamera(CameraUpdateFactory.newLatLng(mLatLng));
getMap().addMarker(new MarkerOptions().position(mLatLng).title(mLatLng.toString()).draggable(false));
}

The way that comes to my mind is that.
1) You have the latlng of the current position ... add marker with custom drawable on it.
2) Disible current location on googlemap..
TO disable it think the call is
mGoogleMapObject.setLocationEnable(false);
Thats it.

Related

Move the marker along the with map as the user moves

I have implemented google map into my app. I have added a custom marker at my current location.I want to move the marker as the user moves, along with the map. I want to implement the functionality as in google maps navigation. While googling i found the following links but did not get the result. so can any one help me out ?
I have added this in onLocationChanged()
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
sourceLocation = latLng;
startPoint = new GeoPoint(latLng.latitude,latLng.longitude);
forNavigation = location;
// animation
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
// mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mPositionMarker.getPosition(), 15));
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
.getLatitude(), location.getLongitude())));
These are the links which i found
Google map: moving marker and map together smoothly along with user?
How to smoothly keep moving current location marker in Google Maps v2 android
here is two solution you have.
Use onlocationtrue then it show you blue dot as your current location.
Use fused api for current location and create you marker in locationchanged.
for fused api you can check from here.
thanks

Reveal markers around a selected location on Google Maps

I am currently coding on Android Studio for Google Maps.
I made the search function working based on the text searched on the search bar. And once the user presses "Search" the map will zoom into that selected location and place a coloured marker.
However, I would like to do it like this:
Once you select the location, you will reveal several markers based on a certain radius/ certain area.
You can add the markers to your map as invisible and compute the distance between your desired location and each marker to show those nearer than a given distance.
private List<Marker> markers = new ArrayList<>(); // List to hold your markers
Add the markers to the map and to the list:
Marker marker = mMap.addMarker(new MarkerOptions().position(yourPosition).visible(false));
markers.add(marker);
And then create a function that, given a LatLng computes the distance to each marker (I'm using SphericalUtil.computeDistanceBetween from the Google Maps Android API Utility Library) and shows the desired markers:
private void showMarkers(LatLng location, float distance) {
for(Marker marker : markers) {
if (SphericalUtil.computeDistanceBetween(marker.getPosition(), location) <= distance) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
}

Android Google Map multiple markers on same location

I have a simple app with Google Map and dynamicly loading markers. In case 2 or more markers have same lat and long only one is presented. Is there any solution for this? This is how I plot markers to map
private void plotMarkers(ArrayList<MyMarker> markers, String markerIcon)
{
if(markers.size() > 0)
{
for (MyMarker myMarker : markers)
{
// Create user marker with custom icon and other options
// Toast.makeText(getApplicationContext(), myMarker.getmLatitude().toString(), Toast.LENGTH_SHORT).show();
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory.fromResource(manageMarkerIcon(markerIcon)));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
Even if you could, how would you use these two overlapping markers? only the top one would be visible.
Instead, if two markers overlap, you could express the data they share in one marker that contains them both.
this answer might assist: Android Google Maps v2 - Add object to marker
Both markers are shown, but both on the same location and it seems to be only one, and if you click on them, only one info window will be presented.
If you really need to add two markers at the same location you may want to take a look at Google Maps Android Marker Clustering Utility. It's part of the Google Maps Android API Utility Library.
A possible workaround could be to check if there is another marker in the position you are trying to draw the marker into, and if so, move the new marker a little bit appart.

Android Google Maps V2 - Indicate if there are markers outside of the current VisibleRegion on screen

I've been trying to work out a method for my application to indicate when there are markers on the map that are outside of the current view/screen/bound/VisibleRegion.
For example, if there are current 5 markers on the map but the user is zoomed into a part of the map where they can't see any of the markers then I would like to be able to flag up to the user that there are markers on the map that they cannot see or something along those lines (it would be nice to be able to indicate in which direction the markers are from the current position).
I thought of using
LatLngBounds currentScreen = googleMap.getProjection()
.getVisibleRegion().latLngBounds;
but this would only be able to tell me which markers are in the current visibleRegion.
any help would be appriciated, thanks.
First, you should save all your markers somewhere. For example, in list
List<Marker> markers = new ArrayList<>();
Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(55.123, 36.456)));
markers.add(marker);
After this you can check, are there markers outside your screen
LatLngBounds currentScreen = mMap.getProjection().getVisibleRegion().latLngBounds;
for(Marker marker : markers) {
if(currentScreen.contains(marker.getPosition())) {
// marker inside visible region
} else {
// marker outside visible region
}
}

code to add markers to map using android google maps v2

I have lat&long values from database.how to display markers based on lat &long values using android google map api v2.In original android google maps,we display markers based on concept itemoverlay. In v2,I dont know how to display markers.
dbAdapter.open();
Cursor points = dbAdapter.clustercall(Btmlft_latitude, toprgt_latitude,
Btmlft_longitude, toprgt_longitude, gridsize1);
int c = points.getCount();
Log.d("count of points", "" + c);
if (points != null) {
if (points.moveToFirst()) {
do {
int latitude = (int) (points.getFloat(points
.getColumnIndex("LATITUDE")) * 1E6);
int longitude = (int) (points.getFloat(points
.getColumnIndex("LONGITUDE")) * 1E6);
mapView.addMarker(new MarkerOptions().position(
new LatLng(latitude, longitude)).icon(
BitmapDescriptorFactory.defaultMarker()));
} while (points.moveToNext());
}
}
points.close();
dbAdapter.close();
That is mycode.I am getting lat&long values from database,but how to add markers based on lat &long values to map.
I read the android google maps api v2.In that have only give statically added data of markers
Use the Marker Class
An icon placed at a particular point on the map's surface. A marker icon is drawn oriented against the device's screen rather than the map's surface; i.e., it will not necessarily change orientation due to map rotations, tilting, or zooming.
A marker has the following properties:
Anchor
The point on the image that will be placed at the LatLng position of the marker. This defaults to 50% from the left of the image and at the bottom of the image.
Position
The LatLng value for the marker's position on the map. You can change this value at any time if you want to move the marker.
Title
A text string that's displayed in an info window when the user taps the marker. You can change this value at any time.
Snippet
Additional text that's displayed below the title. You can change this value at any time.
Icon
A bitmap that's displayed for the marker. If the icon is left unset, a default icon is displayed. You can specify an alternative coloring of the default icon using defaultMarker(float). You can't change the icon once you've created the marker.
Drag Status
If you want to allow the user to drag the marker, set this property to true. You can change this value at any time. The default is true.
Visibility
By default, the marker is visible. To make the marker invisible, set this property to false. You can change this value at any time.
GoogleMap map = ... // get a map.
// Add a marker at San Francisco.
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
You have to use the method GoogleMap.addMarker(MarkerOptions);
I've explained this in a tutorial on my blog:
http://bon-app-etit.blogspot.be/2012/12/add-informationobject-to-marker-in.html
On this blog, you'll find some more posts concerning the new Maps API:
Move the map to current or some location
Create the custom InfoWindow
Associate geodata or objects to a marker
I'm guessing the issue is that you are multiplying your latitude/longitude values by 1E6 instead of dividing them by 1E6. If your stored values came from GeoPoints, that might be your issue. Otherwise, the way you are adding the Markers is fine and should work.
i.e. the lat/lng values you pass to new LatLng() should look like 45.4,-95.5 NOT 45400000,-95500000
Check out the following code:
//mMap is a GoogleMap and point is a GeoPoint
this.mMap.addMarker(getMarker(point, drawableId))
...
public MarkerOptions getMarker(GeoPoint point, int drawableId)
{
return new MarkerOptions()
.position(new LatLng(point.getLatitudeE6() / 1000000.0, point.getLongitudeE6() / 1000000.0))
.icon(BitmapDescriptorFactory.fromResource(drawableId));
}

Categories

Resources