Xamarin Forms and maps - getting coords on map tap, creating routes?

前端 未结 1 661
梦毁少年i
梦毁少年i 2021-01-06 10:48

I use the latest version of Xamarin Forms for my application. This app has a tense integration with maps. Android and iOS are two supported platforms.

Right now I\'m

相关标签:
1条回答
  • 2021-01-06 11:22

    You will need to look up the native iOS and Android implementations to do the work but you should use dependency injection from your SAP or PCL to call to the native implementations for what you need.

    *Note - this is to provide a general outline of how to go about this it is not a copy paste implementation. Open map is not implemented nor are the instructions of how or when to grab the coordinates in the shared code.

    Example

    In shared code -

    interface IMapping
    {
        public OpenMap(); 
        public Model<GeoPoint>GetLastTouchCoordinates();         
    }
    

    In Android project -

    //For use with Xamarin.Forms.DependencyService will not be necessary with other IoC containers if not using forms look up Tiny IoC, Unity or one of the other DI containers
    [assembly: Xamarin.Forms.Dependency(typeof(YourNameSpace.Droid.YourClass))] 
    
    public class GoogleMap : FragmentActivity, IMapping, IOnMapClickListener
    
    {
     private GoogleMap mMap;
     private Point lastClickedPoint;
    
    protected void OnCreate()
    {
        base.OnCreate();
        setContentView(some map fragment layout)
    
        //set the map touch listener
    }
    
    private void handletouch()// implementation of IOnMapClickListener Interface
    {
        //google how to get the coordinates in android on a map touch
        lastClickedPoint = new Point();
        lastClickedPoint.lat = args.lat;
        lastClickedPoint.long = args.long;
    }
    
    private Model<GeoPoint> GetLastTouchCoordinates()
    {
        if(lastclickedPoint != null)
        {
        Model<Geopoint> gp = new Model<Geopoint>();
        gp.latitude = p.latitude;
        gp.longitude = p.longitude;
        return gp;
        }
        else
        {
          return Point with some indication that you have no point
        }
    
    }
    }
    

    In Shared Code --

    Now to use it simply write something like this

    DependencyService.Get<IMapping>.OpenMap()
    DependencyService.Get<IMapping>.GetLastTouchCoordinates()
    

    Link to more details about Dependency Injection in cross platform code http://adventuresinxamarinforms.com/2014/11/17/creating-a-xamarin-forms-application-part-4-dependency-injection/

    0 讨论(0)
提交回复
热议问题