Can't resolve mLatLng in Center of the circle - android

In my Android Map I simply draw a circle and I need the current location to be the center of the circle and when change the location of the device, the circle must be rechanged and load markers again inside the circle.
Here is my code.
First I simply hard coded some 5 places.
LatLng POINTA = new LatLng(6.9192, 79.8950);
LatLng POINTB = new LatLng(6.9006, 79.8533);
LatLng POINTC = new LatLng(6.9147, 79.8778);
LatLng POINTD = new LatLng(6.9036, 79.9547);
LatLng POINTE = new LatLng(6.8397, 79.8758);
Then here is the place I draw the circle and load markers.
public static boolean isMyLocationSet = false; // Get Current location ass default location
#Override
public void onMyLocationChange(Location location) {
isMyLocationSet=false; // Get Current location ass default location
Location target = new Location("target");
for(LatLng point : new LatLng[]{POINTA,POINTB,POINTC,POINTD,POINTE}){
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);
LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = mLocationManager.getBestProvider(criteria, true);
Location currentLocation = mLocationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng mLatLng = new LatLng(latitude, longitude);
Circle circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(mLatLng))
.radius(10000)
.strokeColor(Color.BLUE)
.strokeWidth(2));
target.setLatitude(point.latitude);
target.setLongitude(point.longitude);
Marker marker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(point.latitude, point.longitude))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));
float[] distance = new float[2];
if(location.distanceTo(target) < 10000) {
marker.setVisible(true);
}
else {
marker.remove();
}
}
}
Here I cannot resolve mLatLng in center(new LatLng(mLatLng)) It says
Latlng(double, double) in Latlng cannot be applied to (com.google.andrdoid.gms.maps.model.Latlng)
What should I do for this and Is there anything I did wrong with the code?

you put LatLng object in create new LatLng class object
so given this type of message.Pls use only object as parameter
like
LatLng mLatLng = new LatLng(latitude, longitude);
Circle circle = mMap.addCircle(new CircleOptions()
.center(mLatLng)
.radius(10000)
.strokeColor(Color.BLUE)
.strokeWidth(2));
OR
Circle circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(latitude, longitude))
.radius(10000)
.strokeColor(Color.BLUE)
.strokeWidth(2));

Related

Google maps adding a new marker every update

been trying for a while to sort my maps problem, which is the marker. every time the map updates it adds a new marker. this leaves several markers on the same location.
I want to get this sorted before adding other content like directions.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;//
}
mMap.setMyLocationEnabled(true); // shows location on map
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new LocationUpdateListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
MarkerOptions options = new MarkerOptions() // This adds in a marker
.position(latLng)
.title("Reverse Geo Toast here ???"); // when marker clicked, it will display you are here
mMap.addMarker(options);
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
float zoomLevel = (float) 10; //This zooms into the marker
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
}
You can update the position of an existing marker using setPosition (Reference):
private Marker marker;
// ...
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
if (marker == null) {
MarkerOptions options = new MarkerOptions() // This adds in a marker
.position(latLng)
.title("Reverse Geo Toast here ???"); // when marker clicked, it will display you are here
marker = mMap.addMarker(options);
}
else {
marker.setPosition(latLng);
}
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
float zoomLevel = (float) 10; //This zooms into the marker
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoomLevel));
}

How to add Marker in Google map

I am trying to add Google maps in my application. I get maps successfully displayed in my application.
But the Problem is I am not getting my desired position.
static final LatLng vrrittihLocation = new LatLng(23.001779, 72.618946);
private GoogleMap googleMap;
if (googleMap == null) {
googleMap = ((SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
MarkerOptions Mo = new MarkerOptions()
.position(vrrittihLocation)
.title("Vrrittih Global Recruitment Consulting")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_marker));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(vrrittihLocation, 18);
googleMap.animateCamera(cameraUpdate);
googleMap.addMarker(Mo);
Am I missing something here?
The marker should be on ur maps, the reason u dont see your desired position maybe you can move your camera with following code.
CameraPosition cameraPosition = new CameraPosition.Builder().target(vrrittihLocation)
.zoom(18)
.build();
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(cameraPosition);
googleMap.animateCamera(cameraUpdate);
Get Latitude and Longitude through LocationManager , Like Following ,
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
and in overiden method onLocationChanged(...),
#Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
Hope this helpful to you...
googleMap.moveCamera(cameraUpdate);

Get Google Maps to automatically zoom in on my Current Location?

Possible duplicate of Google Maps v2 - set both my location and zoom in
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g_maps);
GoogleMap googleMap;
LatLng myPosition;
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = fm.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
myPosition = new LatLng(latitude, longitude);
}
}
}
I've tried adding:
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(latitude,
longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
or
googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) );
but Google Maps doesn't automatically zoom in once I've hit the button which calls this method. I still get the Google UI which I can click to zoom in on my Location, but I want it to automatically zoom in.
Any help please?
Try this is simple solution for your question
LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);
and also..
It's possible to change location, zoom, bearing and tilt in one go. It is also possible to set the duration on the animatecamera call.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MOUNTAIN_VIEW) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Have a look at the docs here:
https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera
GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
Location locationCt;
LocationManager locationManagerCt = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationCt = locationManagerCt
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng latLng = new LatLng(locationCt.getLatitude(),
locationCt.getLongitude());
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(latLng)
.title("My Spot").snippet("This is my spot!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)));
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
In your XML file
<Button
android:id="#+id/Bzoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onZoom"
android:text="^" />
<Button
android:id="#+id/Bzoomout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:onClick="onZoom"
android:text="v" />'
In your Java file
public void onZoom(View view)
{
if(view.getId() == R.id.Bzoomin)
{
mMap.animateCamera(CameraUpdateFactory.zoomIn());
}
if(view.getId() == R.id.Bzoomout)
{
mMap.animateCamera(CameraUpdateFactory.zoomOut());
}
}
For Kotlin
val location = CameraUpdateFactory.newLatLngZoom(latlng, 15F)
mMap.animateCamera(location)

How to get Circle Bounds on Google Maps V2

I am trying to find out if the current location of the user is inside a circle. I have this code right now.
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(14.635594, 121.032962))
.radius(80)
.strokeColor(Color.RED)
);
map.setOnMyLocationChangeListener(this);
Location.distanceBetween(
circle.getCenter().latitude, circle.getCenter().longitude,pLat,pLong, distance);
if( distance[0] > circle.getRadius() ){
Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
}
public void onMyLocationChange(Location location) {
// TODO Auto-generated method stub
pLat = location.getLatitude();
// Getting longitude of the current location
pLong = location.getLongitude();
The toast displays outside even though I am clearly inside the circle. I tried this code
LatLng latlng = new LatLng (pLat,pLong);
QC.contains(latlng);
where QC is a LatLngBounds variable. I wonder if Circle has also contains functionality to know if the user is inside the circle or is there a possible workaround. Thanks!
multiply your latitude and longitude values by 1e6, bcoz those values are in microbytes.
more info about microbyte conversion
if you are using Criteria Please try this
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
// this will animate you to the current location
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(),location.getLongitude()), 13));
protected double latti;
protected double longi;
protected double radius;
1/ MainActivity extends Activity implements LocationListener
2/locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
3///Add circle
circle = map.addCircle(new CircleOptions()
.center(new LatLng(47.974593,0.217905))
.radius(80)
.strokeColor(Color.CYAN));
}
4/radius=circle.getRadius();
5/onLocationChanged(Location location) {
latti = location.getLatitude();
longi = location.getLongitude();
float[] distance = new float[1];
Location.distanceBetween(latti,longi,circle.getCenter().latitude,circle.getCenter().longit ude, distance);
showToast(distance[0]);
}
private void showToast(float distance) {
if(distance>radius)
{
remarque="Alarm Out Zone";
}
else{
remarque="Inside Zone";
}
Toast.makeText(getApplicationContext(),"Latitude:"+latti+"\nLongitude:"+longi+"\n"+remarque,Toast.LENGTH_LONG).show();
}

android locationListener not working?

I am having trouble using the loactionListener in eclipse for android. I have been googling for a while now an I can't seem to see why this shouldn't work. The only thing I can think of is that maybe it is because my testing device has no sim. (the internet is provided via wifi).
I have used this as a reference and still, nothing.
Could anyone help me with this problem.
here is the relevant parts of my activity:
public class MainMenu extends Activity implements LocationListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
theMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
locMan = (LocationManager)getSystemService(LOCATION_SERVICE);
}
#Override
public void onLocationChanged(Location location) {
final Double lat = location.getLatitude();
final Double lng = location.getLongitude();
LatLng lastLatLng = new LatLng(lat, lng);
String title = getString(new StringLang().textSet(userLang,"marker_title"));
String snippit = getString(new StringLang().textSet(userLang,"marker_snip"));
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title(title)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.snippet(snippit));
reverseGeoCode(lat, lng);
}
}
I was using a different method to display my location on the map, which worked well but it never updated. It always showed my location at the last place I used the GPS, which turns out was 60mile accross the country. I can see this is working but is there a better way of doing this.
Old method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
if(findViewById(R.id.the_map) != null){
//map has loaded continue
theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
theMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
android.location.Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng lastLatLng ;
if(lastLoc == null){
/* Use the LocationManager class to obtain GPS locations */
double latitude = 0;
double longitude = 0;
lastLatLng = new LatLng(latitude, longitude);
lat = latitude;
lng = longitude;
String title = getString(new StringLang().textSet(userLang,"marker_title"));
String snippit = getString(new StringLang().textSet(userLang,"marker_snip"));
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title(title)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.your_loc_icon))
.snippet(snippit));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lastLatLng) // Sets the center of the map to user position
.zoom(0) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(20) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
currentLoc = lastLatLng;
theMap.animateCamera (CameraUpdateFactory.newCameraPosition(cameraPosition), 3000, null);
final TextView geoTagText = (TextView)findViewById(R.id.text_geoTag);
geoTagText.setText("We cannot find your current location, please check your settings.");
}else{
double latitude = lastLoc.getLatitude();
double longitude = lastLoc.getLongitude();
lastLatLng = new LatLng(latitude, longitude);
lat = latitude;
lng = longitude;
animateMap(lastLatLng);
}
}else{
theMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.the_map)).getMap();
theMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
android.location.Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
LatLng lastLatLng;
double latitude = lastLoc.getLatitude();
double longitude = lastLoc.getLongitude();
lastLatLng = new LatLng(latitude, longitude);
lat = latitude;
lng = longitude;
animateMap(lastLatLng);
//no map to load
}
}
#Override
public void onLocationChanged(Location location) {
final Double lat = location.getLatitude();
final Double lng = location.getLongitude();
LatLng lastLatLng = new LatLng(lat, lng);
String title = getString(new StringLang().textSet(userLang,"marker_title"));
String snippit = getString(new StringLang().textSet(userLang,"marker_snip"));
if(userMarker!=null) userMarker.remove();
userMarker = theMap.addMarker(new MarkerOptions()
.position(lastLatLng)
.title(title)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))
.snippet(snippit));
}
it would also be useful to add that there is button to relocate the user manually if they want.
reLocBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(menuActive == true){
playSound();
LocationManager loc = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
android.location.Location gpsLoc = (Location) loc.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double lat = gpsLoc.getLatitude();
double lng = gpsLoc.getLongitude();
LatLng lastLatLng = new LatLng(lat, lng);
animateMap(lastLatLng);
}
}
});
I'm not to sure why either method doesn't update, but the second method seams to work better on first load.
You don't appear to be using requestLocationUpdates() anywhere. In that method you pass a LocationListener object that it then calls for location updates.
i.e. in your case:
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
If you are using Google Maps API v2, you can do it using that, for example:
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));
if(mMap != null){
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
}
};
and then set the listener for the map:
mMap.setOnMyLocationChangeListener(myLocationChangeListener);
This will get called when the map first finds the location.
No need for LocationService or LocationManager at all.

Categories

Resources