The repeat markers in positioning (Android API-2)? - android

I have a code which determine my position on google map by a marker when the position is changed the previous marker remains and it repeats in the new position. I want to have only marker when updating the position.
Here is my code :
public class MainActivity extends FragmentActivity {
private GoogleMap map;
Marker marker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
networkLocation();
}
private void animateToLocation(double latitude, double longtitude) {
LatLng latlang = new LatLng(latitude, longtitude);
CameraPosition position1 = CameraPosition.builder().target(latlang)
.zoom(15).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(position1));
marker = map.addMarker(new MarkerOptions().title("TEST").position(
latlang));
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(latlang, 100));
}
public void networkLocation() {
G.locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
animateToLocation(location.getLatitude(),
location.getLongitude());
}
});
}
}

Insert map.clear(); befor :
marker = map.addMarker(new MarkerOptions().title("TEST").position(latlang));
And your result is :
map.clear();
marker = map.addMarker(new MarkerOptions().title("TEST").position(latlang));
And you can use your code...Correctly.
Good look

Seems like you are using
marker = map.addMarker(new MarkerOptions().title("TEST").position(
latlang));
which actually adds a new marker on the map each time animateToLocation(lat, lng) is called. What you need to actually do is call
marker.setPostion(latLng);
there instead and initialize the marker somewhere at the beginning of your activity.

Related

android: Can't add current location on map

I'm having trouble putting my current/updated location on the map which has other data. please see the image below. I need it because I will do a distance between the vehicle location and my current location, anyways how to do it? this activity is extended by fragment. I really need your help.
This is my OnMapReady:
public void onMapReady(GoogleMap googleMap) {
mMapProximity = googleMap;
mMapProximity.setBuildingsEnabled(true);
mMapProximity.getUiSettings().setRotateGesturesEnabled(false);
setUpMap();
}
private void setUpMap() {
mMapProximity.clear();
mMapProximity.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mMapProximity.setIndoorEnabled(true);
mMapProximity.setBuildingsEnabled(true);
mMapProximity.getUiSettings().setZoomControlsEnabled(false);
final Double lati = Double.parseDouble(latitudeProxListMap);
final Double longi = Double.parseDouble(longitudeProxListMap);
m = mMapProximity.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_logo))
.visible(true));
mMapProximity.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker.showInfoWindow();
}
});
mMapProximity.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lati, longi), 17.0f));
mMapProximity.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
return true;
}
});
mMapProximity.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
m.showInfoWindow();
}
class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(R.layout.info_vehicle_list_map, null);
TextView title =v.findViewById(R.id.title);
TextView snippet =v.findViewById(R.id.snippet);
snippet.setVisibility(View.GONE);
title.setText("Location: " + location);
return v;
}
}
Implement LocationListener in your activity and than in onLocationChange method update the marker with the current location values. That will put a marker on the current location.

How to show pin's latitude and longitude on map (Android Studio) [duplicate]

This question already has answers here:
Get Latitude and longitude of marker in google maps
(2 answers)
Closed 5 years ago.
How to get latitude and longitude from pin that user dropped ? Like Google map, we click a pin on the map , it shows the latitude and longitude about the pin. Could anyone help me ? Thanks a lot.
Like the picture ,it shows latitude and longitude .
Images
public class YOURACTIVITY extends AppCompatActivity implements OnMapReadyCallback
private GoogleMap mMap;
<--
OnCreate
SupportMapFragment map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
map.getMapAsync(this);
-->
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
double mLat = LATITUDE;
double mLon = LONTITUDE;
LatLng mylocation = new LatLng(mLat, mLon);
mMap.addMarker(new MarkerOptions().position(mylocation).snippet("TEXT BELLOW TITLE").title("TITLE")).showInfoWindow();
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation,15));
}
UPDATE
Try to do add something like this. Create custom layout and add information
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
View view = MainActivity.getLayoutInflater().inflate(R.layout.map_custom, null);
TextView titleTextView = (TextView) view.findViewById(R.id.map_title);
TextView snippetTextView = (TextView) view.findViewById(R.id.map_snippet);
titleTextView.setText(marker.getTitle());
snippetTextView.setText(marker.getSnippet());
return view;
}
});
Try this efficient solution:
public void onMapLongClick(LatLng point) {
pin.setText("New marker added#" + point.toString());
//Create a marker object
Marker myMarker = map.addMarker(new MarkerOptions().position(point).title(point.toString()));
//And now you can use it's values
myMarker.getPosition().latitude;
myMarker.getPosition().longitude;
}
You can use the setOnMarkerDragListener method:
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
#Override
public void onMarkerDragStart(Marker marker) {
}
#Override
public void onMarkerDrag(Marker marker) {
}
#Override
public void onMarkerDragEnd(Marker marker) {
double latitude = marker.getPosition().latitude;
double longitude = marker.getPosition().longitude;
}
});
The short of it is to use the Marker.getPosition() method, maybe in a toast:
#Override
public void onMarkerDragEnd(Marker marker) {
LatLng pos = marker.getPosition();
Toast.makeText(MainActivity.this, "Point coordinates: " + pos.latitude
+ ", " + pos.longitude, Toast.LENGTH_LONG).show();
}

Google map i am getting current location when i am change location marker does not moves in google map

Here is my code:
public class MapViewFragment extends Fragment implements OnMapReadyCallback {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.maps, container, false);
Context context =this.getContext();
GPSService mGPSService = new GPSService(context);
mGPSService.getLocation();
if (mGPSService.isLocationAvailable == false) {
} else {
// Getting location co-ordinates
lat1 = mGPSService.getLatitude();
lng1 = mGPSService.getLongitude();
}
mMapView.onCreate(savedInstanceState);
ActivityCompat.requestPermissions(getActivity(),new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
call.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
call();
}
});
mMapView.getMapAsync(this); //this is important
}
});*/
}
return v;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
m1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(lat1,lng1))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.anchor(0.5f, 0.5f)
.title("Title1")
.snippet("Snippet1"));
#Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
#Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
#Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
#Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
}
In program i getting current location lat and lang.When i moved to other location marker does not change. whenever I move the location has to change.
Current location value iam getting whenever i move location value does not change.want implement any method change the location.
i assume you've already created a marker to indicate your current position. like this
Marker currentLocation;
Now You need to implement LocationListener and update your MapView in the onLocationChanged callback
#Override
public void onLocationChanged(Location location) {
if(currentLocation != null){
currentLocation.remove();
}
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
// Getting latitude of the current location
double latitude = location.getLatitude();
// Getting longitude of the current location
double longitude = location.getLongitude();
// Creating a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
currentLocation = googleMap.addMarker(new MarkerOptions().position(latLng)));
// Showing the current location in Google Map
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

I want to replace a Marker from a map when user visits that location ( gets near a Marker )

First, I plot a Marker like this:
public void addMarker(String title,String lat,String Lng,int id,String address,int f)
{
marker= mMap.addMarker(new MarkerOptions().snippet(title)
.title(title+", "+address)
.position(new LatLng(Double.valueOf(lat), Double.valueOf(Lng)))
.icon(BitmapDescriptorFactory.fromResource(id)));
LatLng coordinate = new LatLng(Double.valueOf(lat), Double.valueOf(Lng));
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 10);
mMap.animateCamera(yourLocation);
mMarkerArray.add(marker);
}
After that I am trying to replace the Marker with another icon when ever I reached at any existing Location
#Override
public void onLocationChanged(Location location)
{
Log.d("latitude_main", "onlocation???");
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude_main", "latitude--" + latitude+"longitude="+longitude);
current_lat= String.valueOf(latitude);
current_lng= String.valueOf(longitude);
Log.e("latitude_main","size-=="+salesmanlocationArrayList.size() );
for(int i=0;i<salesmanlocationArrayList.size();i++)
{
if(salesmanlocation.getLati().equals("12.9165757") && salesmanlocation.getLongi().equals("77.6101163"))
{
mMap.addMarker(new MarkerOptions()
.snippet(""+i).title(salesmanlocation.getFirm_name()+", "+salesmanlocation.getAddress())
.position(new LatLng(Double.valueOf(salesmanlocation.getLati().toString()), Double.valueOf(salesmanlocation.getLongi().toString())))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.event_events_select)));
}
mapFragment.getMapAsync(this);
}
}
I want to remove the marker from the map when the user visits that location.
You can simply define one OnMyLocationChangeListener class that performs your tasks, and set it on your GoogleMap instance, this way you can use it whenever you want in your application.
Step 1 - define your listener
public class MyMarkerLocationListener implements GoogleMap.OnMyLocationChangeListener {
List<Marker> markerList;
int MY_DISTANCE;
GoogleMap mMap;
public MyMarkerLocationListener(List<Marker> markerList, int meters, GoogleMap mMap)
{
this.markerList = markerList;
this.MY_DISTANCE = meters;
this.mMap = mMap;
}
#Override
public void onMyLocationChange(Location location) {
// your code/logic
//...
Location myNewLocation = location;
Location someMarkerLocation = new Location("some location");
//for each marker on your list
//check if you are close to it
for (Marker m : markerList) {
LatLng markerPosition = m.getPosition();
someMarkerLocation.setLatitude(markerPosition.latitude);
someMarkerLocation.setLongitude(markerPosition.longitude);
if (myNewLocation.distanceTo(someMarkerLocation) < MY_DISTANCE) {
//remove marker
m.remove();
//or if you still want to use it later
//m.setVisible(false);
// add your new marker
//mMap.addMarker(new MarkerOptions().icon()....);
}
}
}
}
After defining your class you just set the listener on your map on your fragment or activity code =)
Step 2 - instanciate the listener and set it
MyMarkerLocationListener myListener = new MyMarkerLocationListener(mMarkerArray, 100, mMap);
mMap.setOnMyLocationChangeListener(myListener);
UPDATE to answer your question in the comments:
You should initialize mMap before using it, take a look at this piece of code from this Stackoverflow question
public class MapPane extends Activity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_activity);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
//DO WHATEVER YOU WANT WITH GOOGLEMAP
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
map.setMyLocationEnabled(true);
map.setTrafficEnabled(true);
map.setIndoorEnabled(true);
map.setBuildingsEnabled(true);
map.getUiSettings().setZoomControlsEnabled(true);
}
}
don't forget your activity should implement the OnMapReadyCallback interface so the onMapReady method is called
you can use the map only after it is ready
Hope this helps!

control user gesture on my map

I new in coding google map,
my question is how i control the user gestur drag , zoom in and zoom out.
because my code always back to the current location of user when i zoomin/out, nad when i drag/ scroll up, down, left, right. always back to the current possition .
its my code for current loc user
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
}
};
You can use a boolean to move the camera only the first time:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
private GoogleMap mMap;
private Marker mMarker;
private boolean firstTime = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
}
#Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMarker = mMap.addMarker(new MarkerOptions().position(loc));
if (firstTime) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
firstTime = false;
}
}
}
NOTE: Take into account that this example uses GoogleMap.OnMyLocationChangeListener only because that is the method that you are using in your question, but it's deprecated and you must use the FusedLocationProviderApi according to the documentation:
public final void setOnMyLocationChangeListener
(GoogleMap.OnMyLocationChangeListener listener)
This method was deprecated. use
com.google.android.gms.location.FusedLocationProviderApi instead.
FusedLocationProviderApi provides improved location finding and power
usage and is used by the "My Location" blue dot. See the
MyLocationDemoActivity in the sample applications folder for example
example code, or the Location Developer Guide.

Categories

Resources