Google maps V2 android get current location - google-maps-android-api-2

Im trying to place a marker on the position i am, this way:
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_container)).getMap();
googleMap.setMyLocationEnabled(true);
My question is, how to get coordinates from myLocation, i tried, googleMap.getMyLocation().getLatitude() and googleMap.getMyLocation().getAltitude() right after googleMap.setMyLocationEnabled(true), but app crashes, another thing i did was Location loc=lm.getLastKnownLocation(provider) but are not the same coordinates and the marker is placed in the wrong place.
How can you people help me?

SupportMapFragment mf =(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mf.getMap();
mMap.setMyLocationEnabled(true);
mMap.setMapType(mMap.MAP_TYPE_NORMAL);

Just add these two lines of code to get current location on Maps
googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
and when you long press on any location on map add this code to get its location
googleMap.setOnMapLongClickListener(new OnMapLongClickListener() {
#Override
public void onMapLongClick(LatLng arg0) {
double alt = arg0.latitude;
double alo = arg0.longitude;
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(alt, alo)).title("Lat ="+alt+" Lang="+alo);
googleMap.addMarker(marker1);
}
});

Related

Get map center point on scrolling Google maps flutter

I'm doing an app in my app. I'm using google maps flutter and I'm having an issue regarding scrolling. When I scroll the map I want to get the center points (long, lat) of the map that is if I scroll the map and stop at certain place it would grab the location points of center.
Can anyone suggest how to solve this issue? It can be very helpful Thank you in advance..
I would get the bounds of the screen, and then find the center of those bounds. If you have a GoogleMapController for the GoogleMap, just use a function like this:
getCenter() async {
LatLngBounds bounds = await mapsController.getVisibleRegion();
LatLng center = LatLng(
(bounds.northeast.latitude + bounds.southwest.latitude) / 2,
(bounds.northeast.longitude + bounds.southwest.longitude) / 2,
);
return center;
}
In the latest version of google maps flutter we can get the coordinates of location by passing in the screen coordinatinates,
// some code here
Size screen = MediaQuery.of(context).size;
// some code here
final coords = await mapController.getLatLng(ScreenCoordinate( x: screen.width/2, y:
screen.width/2));

Unity mapbox - coordinates at center of zoomable map?

I know how to move the zoomable map to a specific lat/long, and that point will be centered in the screen. If the user moves the map (drags it sideways), how do I get the coordinates at the point of the map that is now centered?
Thanks!
I would recommend to use the Camera.ScreenToWorldPoint method from Unity:
https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html?_ga=2.95757022.39200391.1587116665-1246420375.1587116665
Please also have a look at this similar thread:
https://forum.unity.com/threads/how-to-get-a-world-position-from-the-center-of-the-screen.524573/
Thanks!
Based on the similar thread you pointed me to, I added a function to my QuadTreeCameraMovement...
public Vector2d GeoCoordsAtCenter() {
var centerScreen = _referenceCamera.ViewportToScreenPoint(new Vector3(.5f, .5f, _referenceCamera.transform.localPosition.y));
var pos = _referenceCamera.ScreenToWorldPoint(centerScreen);
var latlongDelta = _mapManager.WorldToGeoPosition(pos);
Debug.Log("CENTER: Latitude: " + latlongDelta.x + " Longitude: " + latlongDelta.y);
return latlongDelta;
}

Display points when there's great distance between them (GWT-Openlayers)

The case is the following: I have a layer and there are two points on it. The first is in Australia, the second is in the USA. The continent or the exact position of the points doesn't count. The essential part is the great distance between the points. When the application starts, the first point appears (zoomlevel is 18). The second point isn't displayed because it is far away from here and the zoomlevel is high. Then i call the panTo function with the location of the second point. The map jumps to the right location but the second point doesn't appear. The point appears only if i zoom in/out or resize the browser window. The GWT code:
LonLat center = new LonLat(151.304485, -33.807831);
final LonLat usaPoint = new LonLat(-106.356183, 35.842721);
MapOptions defaultMapOptions = new MapOptions();
defaultMapOptions.setNumZoomLevels(20);
// mapWidget
final MapWidget mapWidget = new MapWidget("100%", "100%", defaultMapOptions);
// google maps layer
GoogleV3Options gSatelliteOptions = new GoogleV3Options();
gSatelliteOptions.setIsBaseLayer(true);
gSatelliteOptions.setDisplayOutsideMaxExtent(true);
gSatelliteOptions.setSmoothDragPan(true);
gSatelliteOptions.setType(GoogleV3MapType.G_SATELLITE_MAP);
GoogleV3 gSatellite = new GoogleV3("Google Satellite", gSatelliteOptions);
mapWidget.getMap().addLayer(gSatellite);
// pointLayer
VectorOptions options = new VectorOptions();
options.setDisplayOutsideMaxExtent(true);
Vector vector = new Vector("layer1", options);
mapWidget.getMap().addLayer(vector);
mapWidget.getMap().addControl(new LayerSwitcher());
mapWidget.getMap().addControl(new MousePosition());
mapWidget.getMap().addControl(new ScaleLine());
mapWidget.getMap().addControl(new Scale());
// two points are added to the layer
center.transform(new Projection("EPSG:4326").getProjectionCode(), mapWidget.getMap().getProjection());
vector.addFeature(new VectorFeature(new Point(center.lon(), center.lat())));
usaPoint.transform(new Projection("EPSG:4326").getProjectionCode(), mapWidget.getMap().getProjection());
vector.addFeature(new VectorFeature(new Point(usaPoint.lon(), usaPoint.lat())));
// the center of the map is the first point
mapWidget.getMap().setCenter(center, 18);
// 3 sec later panTo second point
Timer t = new Timer() {
#Override
public void run() {
mapWidget.getMap().panTo(usaPoint);
}
};
t.schedule(3000);
I tried to reproduce this situation with pure Openlayers, but it worked fine. Here is the link
So i think the problem is with GWT-Openlayers. Has anybody experienced such behaviour? Or has anybody got a solution to this problem?
What a strange problem.
For now I did only found a way around it, but not a real fix. Seems to be a bug in GWT-OL as you say, but I can't imagine where.
What you can do is add the following 3 lines to your code :
mapWidget.getMap().panTo(usaPoint);
int zoom = mapWidget.getMap().getZoom();
mapWidget.getMap().setCenter(usaPoint, 0);
mapWidget.getMap().setCenter(usaPoint, zoom);
(note : I am a contributor to the GWT-OL project, I also informed other contributors of this problem, maybe they can find a better solution)
Edit : Another GWT-OL contributor looked into this but also couldn't find a real solution
but another workaround is to use zoomToExtend for the requested point :
Bounds b = new Bounds();
b.extend(new LonLat(usaPoint.getX(), usaPoint.getY()));
mapWidget.getMap().zoomToExtent(b);

How can I get a fake 'current location' when creating an IOS Map with Xamarin Studio and the iPhone Emulator?

Problem:
When using the Xamarin iPhone emulator, the current location is not getting set on the map.
Details:
I'm trying to plot my current location on a Map, in a sample iPhone app I'm learning with Xamarin Studio and the iPhone emulator.
I have the map displayed but there's no current location getting set.
I did get asked to use my Current Location (which I'm sure I said yes/ok to) .. but it keeps centering it in San Fran, near union square :(
When ever I run my emulato, I see this text pop up:
2013-10-22 09:27:45.018 MyApp [6018:1503] MonoTouch: Socket error while connecting to MonoDevelop on 127.0.0.1:10000: Connection refused
So i'm not sure if that has something to do with it?
Ok, so lets look at some code I've got.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
map.MapType = MKMapType.Standard;
map.ShowsUserLocation = true;
map.ZoomEnabled = true;
map.ScrollEnabled = true;
map.DidUpdateUserLocation += (sender, e) => {
if (map.UserLocation != null)
{
CentreMapAtLocation(map.UserLocation.Coordinate.Latitude,
map.UserLocation.Coordinate.Longitude);
}
// User denied permission, or device doesn't have GPS/location ability.
if (!map.UserLocationVisible)
{
// TODO: Send the map somewhere or hide the map and show another message.
//CLLocationCoordinate2D coords = new CLLocationCoordinate2D(37.33233141,-122.0312186); // cupertino
//MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(20), MilesToLongitudeDegrees(20, coords.Latitude));
//mapView.Region = new MKCoordinateRegion(coords, span);
}
};
private void CentreMapAtLocation(double latitude, double longitude)
{
CLLocationCoordinate2D mapCenter = new CLLocationCoordinate2D (latitude, longitude);
MKCoordinateRegion mapRegion = MKCoordinateRegion.FromDistance (mapCenter, 10000, 10000);
map.CenterCoordinate = mapCenter;
map.Region = mapRegion;
}
So it's nothing too crazy, IMO.
Anyone have any suggestions?
Have you tried setting the custom location within the simulator?
I tend to use a combination of the Custom Location setting and this tool when I need to verify locations within the iOs simulator. You won't need to make any changes to your code for this to work; it just pipes the set location into the location manager within iOs.
To my knowledge, the simulator does not support GPS or WiFi based location therefore it can't use your current location like a physical device. Perhaps someone else can clarify this.
For further information, see:
Set the location in iPhone Simulator
http://bencoding.com/2011/12/28/setting-you-location-in-the-ios-5-0-simulator/

GWT Google Map Api V3 - broken when changing it

It is working fine for me for the first time it is rendered.
But, If I change anything over the map or recreate it, its broken.
Here is the screen shot for how it looks.
Here is a screen shot after I changed the results per page value.
This is my code.
#UiField DivElement mapPanel;
private GoogleMap googleMap;
public void loadAllMarkers(final List<LatLng> markers)
{
if(!markers.isEmpty())
{
final MapOptions options = MapOptions.create();
options.setMapTypeId(MapTypeId.ROADMAP);
googleMap = GoogleMap.create(mapPanel, options);
final LatLngBounds latLngBounds = LatLngBounds.create();
for(LatLng latLng : markers)
{
final MarkerOptions markerOptions = MarkerOptions.create();
markerOptions.setPosition(latLng);
markerOptions.setMap(googleMap);
final Marker marker = Marker.create(markerOptions);
latLngBounds.extend(marker.getPosition());
}
googleMap.setCenter(latLngBounds.getCenter());
googleMap.fitBounds(latLngBounds);
}
}
I am calling the loadAllMarkers() method whenever new results needs to be loaded.
Can someone point out what I am doing wrong here.
This seems to come from the following (which I pulled from a Google+ Community - GWT Maps V3 API):
Brandon DonnelsonMar 5, 2013
I've had this happen and forgotten why it is, but
mapwidget.triggerResize() will reset the tiles. This seems to happen
when the onAttach occurs and animation exists meaning that the div
started smaller and increases in side, but the map has already
attached. At the end of the animation, the map doesn't auto resize.
I'v been meaning to investigate auto resize but I haven't had time to
attack it yet.
In your case, you would call googleMap.triggerResize() after you finish your changes. this solved my problem when I had the exact same issue. I know it's a little late, but I hope it helps!
Another answer there was to extend the Map widget with the following:
#Override
protected void onAttach() {
super.onAttach();
Timer timer = new Timer() {
#Override
public void run() {
resize();
}
};
timer.schedule(5);
}
/*
* This method is called to fix the Map loading issue when opening
* multiple instances of maps in different tabs
* Triggers a resize event to be consumed by google api in order to resize view
* after attach.
*
*/
public void resize() {
LatLng center = this.getCenter();
MapHandlerRegistration.trigger(this, MapEventType.RESIZE);
this.setCenter(center);
}