android - with xamarin forms maps, how can i center the map in the user current location? -
in xamarin cross platform app trying center map current user location, when app starts can't adapt example found downloaded geolocator plugin xamarin couldn't make work.
using system; using system.collections.generic; using system.linq; using system.text; using xamarin.forms.maps; using xamarin.forms; using plugin.geolocator; namespace map_test { public partial class app : application { public app() { map mapa = new map(); mapa.isshowinguser = true; var locator = crossgeolocator.current; var position = locator.getpositionasync(); mapa.movetoregion(mapspan.fromcenterandradius(new position(position.latitude, position.longitude), distance.frommiles(1))); var rootpage = new contentpage(); rootpage.content = mapa; mainpage = rootpage; } protected override void onstart() { // handle when app starts } protected override void onsleep() { // handle when app sleeps } protected override void onresume() { // handle when app resumes } } } i added new contentpage , did don't know how access position.latitude , position.longitude values updateposition method.
using plugin.geolocator; using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.text; using xamarin.forms; using xamarin.forms.maps; using plugin.geolocator; namespace map_test { public class maptest : contentpage { label locationlabel = new label(); public maptest() { map mapa = new map(); mapa.isshowinguser = true; mapa.movetoregion(mapspan.fromcenterandradius(new position(position.latitude, position.longitude), distance.frommiles(1))); content = new stacklayout { children = { mapa } }; } public async void updateposition() { var locator = crossgeolocator.current; var position = await locator.getpositionasync(); } } }
i solved way:
using plugin.geolocator; using system; using system.collections.generic; using system.diagnostics; using system.linq; using system.text; using xamarin.forms; using xamarin.forms.maps; namespace map_test { public class maptest : contentpage { public map mapa = new map(); private async void findme() { var locator = crossgeolocator.current; plugin.geolocator.abstractions.position position = new plugin.geolocator.abstractions.position(); position = await locator.getpositionasync(); mapa.movetoregion(mapspan.fromcenterandradius(new position(position.latitude, position.longitude), distance.frommiles(1))); } public maptest() { mapa.isshowinguser = true; findme(); var position = new position(-38.743146, -72.615408); var pin1 = new pin { type = pintype.generic, position = position, label = "estacionamiento en pasaje a", address = "custom detail info" }; mapa.pins.add(pin1); content = new stacklayout { children = { mapa } }; } } }
Comments
Post a Comment