Android Google Maps Route Between 2 Points - java

I'm trying to to display route between 2 point, but it seems that its tougher than I though, although there are enough posts on the subject.
This is the code I have
public class MainActivity extends FragmentActivity implements OnMapReadyCallback
{
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
mMap = googleMap;
Context context;
LatLng Current = new LatLng(55.0567842,-1.599155200000041);
LatLng Target = new LatLng(55.0239304,-1.5453314000000091);
LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(Current);
boundsBuilder.include(Target);
LatLngBounds bounds = boundsBuilder.build();
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 3));
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(ContextCompat.getColor(context, R.color.colorPrimary));
polyOptions.width(10);
polyOptions.add(Current, Target);
}
Any help with this would be much appreciated.

Related

Android - Add a Marker to a Fragment Map from MapsActivity

I have the problem which I write in the title
Here are the 2 codes from MapsActivity and fragment
#Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// redraw the marker when get location update.
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(latlng).title("Marker").snippet(Float.toString(location.getSpeed())));
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 15, locationListener);
}
}
Fragment Map
public class Tab2Map extends Fragment implements OnMapReadyCallback {
GoogleMap map;
public Tab2Map() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_tab2, container, false);
return v;
}
#Override
public void onViewCreated(View view,#Nullable Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
SupportMapFragment mapFragment=(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.fragment);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
Ηow can I put the marker from MapsActivity into the fragment because it's the only way to print it in map.App has Action Bar Tabs and the map can be displayed only through fragment?
Call this method in onMapReady(GoogleMap googleMap)
private void updateMap(List<SubOrdinateLocation> locations) {
if (markers != null) {
markers.clear();
mGoogleMap.clear();
}
for (SubOrdinateLocation sol : locations) {
if (sol.latitude != 0.0 && sol.longitude != 0.0) {
LatLng sydney = new LatLng(sol.latitude, sol.longitude);
Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(sydney).title(sol.subOrdinateId).snippet(sol.subOrdinateName));
markers.add(marker);
}
}
if (markers != null && markers.size() > 0) {
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
LatLng center = bounds.getCenter();
builder.include(new LatLng(center.latitude - 0.001f, center.longitude - 0.001f));
builder.include(new LatLng(center.latitude + 0.001f, center.longitude + 0.001f));
bounds = builder.build();
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mGoogleMap.animateCamera(cu);
}
}
Modify it as per your needs.

cannot add titles over places in google maps?

I've put some red marks over some places but I can not put texts over every place so that a different name and number is protected
As an example I want to type above each tag :
Grup_num + Index Number
Code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setZoomControlsEnabled(true);
// Add a marker in Sydney and move the camera
// LatLng Muscat = new LatLng(23.5840481, 58.388295);
double[] myListl = {25.696042,25.696847,25.697214};
double[] myListr = {32.649797,32.649693,32.651163};
//25.696042,32.649797
//25.696847,32.649693
//25.697214,32.651163
for(int i=0; i <myListl.length; i++) {
// Add a marker in Sydney and move the camera
LatLng Muscat = new LatLng(myListl[i], myListr[i]);
mMap.addMarker(new MarkerOptions().position(Muscat).title("Grup_num"+i).icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_location_1))).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Muscat, 16));
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
Toast.makeText(MapsActivity.this, latLng.latitude+","+latLng.longitude, Toast.LENGTH_SHORT).show();
mMap.clear();
mMap.addMarker(new MarkerOptions().position(latLng).title("MyLocation")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_location_1))).showInfoWindow();
}
});
}
}

Info Windows in Google map doesn't show pictures- android studio

I've been trying to show some info whith info windows -in my app which uses google map- with InfoWindowAdapter but it just does'nt show any pictures so whats the problem?
here's my code for it
public class findonmap extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Bundle recevier=getIntent().getExtras();
float a=recevier.getFloat("x");
float b=recevier.getFloat("y");
LatLng mycordinate = new LatLng(a,b);
mMap.addMarker(new MarkerOptions().position(mycordinate).icon(BitmapDescriptorFactory.
fromResource(R.drawable.marker2)));
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
return null;
}
#Override
public View getInfoContents(Marker marker) {
// ConstraintLayout layout=(ConstraintLayout)findViewById(R.id.indo)
View view=getLayoutInflater().inflate(R.layout.info,null);
return view;
}
});
int zoomLevel = 4; //This goes up to 21
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mycordinate, zoomLevel));
}}

Google maps V2 android tiles do not show after a certain zoom level

I'm new to Google maps. Why is is that when I'm trying to zoom in (either with controls or by tapping), after a - relatively high - zoom level the tiles stop loading and all I see is a pixelated view.
Here is my complete activity:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private SupportMapFragment mapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onResume() {
System.out.println("Onresume");
mapFragment.onResume();
super.onResume();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng center = new LatLng(47.49801, 19.03991);
mMap.addMarker(new MarkerOptions().position(center).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(center));
}
}
use value required value for zoom laval
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLocation, 5f));

Cannot get markers from Firebase

I cannot get markers on my map from Firebase. I have tried a few examples I found but I can't seem to get it to work. I want all of the locations that I have stored in FireBase to be on one map.
Code:
public class AllJobsMapActivity extends FragmentActivity implements OnMapReadyCallback {
private DatabaseReference geoDatabase = FirebaseDatabase.getInstance().getReference("places");
GoogleMap mapFragment;
List<Marker> markerList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_job_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
geoDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child : dataSnapshot.child("places").getChildren()) {
String latitude = child.child("lattitude").getValue().toString();
String longitude = child.child("longitude").getValue().toString();
double Latitude = Double.parseDouble(latitude);
double Longitude = Double.parseDouble(longitude);
String name = child.child("name").getValue().toString();
LatLng cod = new LatLng(Latitude, Longitude);
googleMap.addMarker(new MarkerOptions().position(cod).title(name));
}
}
#Override
public void onCancelled(DatabaseError firebaseError) {
}
});
}
}
Firebase Tree:

Categories

Resources