android: Can't add current location on map - android

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.

Related

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));
}

ClusterClick Listener not in Android

I am creating an app where I am clustering multiple request on maps.
I am able to cluster request on map but I want to get click event on cluster. I tried using setOnClusterItemClickListener but I am unable to capture click event in that method.I am also having OnCameraChangeListener in map which runs when I tap on cluster.
Is this causing issue because of both listeners or I am doing something wrong.
Here's is my listener's code
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
cameraChangeHandler.removeCallbacks(throttledRunnable);
cameraChangeHandler.postDelayed(throttledRunnable, 300);
if (marker != null) {
marker.remove();
mClusterManager.clearItems();
}
LatLng latLng = cameraPosition.target;
appSharedPreference.setLatitude(String.valueOf(latLng.latitude));
appSharedPreference.setLongitude(String.valueOf(latLng.longitude));
marker = map.addMarker(new MarkerOptions().position(latLng));
submitData();
}
});
map.setOnMarkerClickListener(mClusterManager);
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem myItem) {
Toast.makeText(getActivity(),"Hello",Toast.LENGTH_LONG).show();
return false;
}
});
Using the link provided I was able to get this working using following code:
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition cameraPosition) {
if (marker != null) {
marker.remove();
mClusterManager.clearItems();
}
LatLng latLng = cameraPosition.target;
appSharedPreference.setLatitude(String.valueOf(latLng.latitude));
appSharedPreference.setLongitude(String.valueOf(latLng.longitude));
// marker = map.addMarker(new MarkerOptions().position(latLng));
submitData();
pDialouge.hide();
}
});
mClusterManager.setRenderer(new MyClusterRenderer(getActivity(), map, mClusterManager));
map.setOnMarkerClickListener(mClusterManager);
mClusterManager
.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
#Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
clickedCluster = cluster;
return false;
}
});
mClusterManager
.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem item) {
clickedClusterItem = item;
return false;
}
});

Marker in google maps for android

I want to know how i can set "OnClick" for a marker on google maps in android.
Example: User touch the marker and My app detect this.
My marker
#Override
public void onMapReady(GoogleMap map) {
map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(-18.142, 178.431), 2));
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
map.setMyLocationEnabled(true);
mUiSettings = map.getUiSettings();
mUiSettings.setZoomControlsEnabled(true);
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
Try with OnMarkerClickListener, like following:
map.setOnMarkerClickListener(new OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker arg0) {
// do your stuff
return true;
}});
implement OnMarkerClickListener
public class MarkerDemoActivity extends android.support.v4.app.FragmentActivity
implements OnMarkerClickListener
{
private Marker myMarker;
myMarker = googleMap.addMarker();
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Example
this.googleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker arg0) {
Log.d(TAG, arg0.getTitle() + " | " + arg0.getPosition().toString());
return false;
}
});

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

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.

Categories

Resources