Skip to content

Single manipulatable 3d model on map

jaakla edited this page Oct 19, 2014 · 4 revisions

Sometimes you wish to add individual 3D models on map. In following sample we add a car to map, inspired by http://earth-api-samples.googlecode.com/svn/trunk/demos/milktruck/index.html .

Milk truck screenshot

  1. Download SDK latest development snapshot
  2. Download the milk truck model milktruck.nml in NML format. original source
  3. Copy jar file to libs folder and nml file to res/raw folder of your project
  4. Add model to a layer on the map, e.g. in onCreate() method:
        // have a field with model, so you can update it later in inner class
    private NMLModel milkTruck;
   ...

     // define style
        ModelStyle modelStyle = ModelStyle.builder().build();
        StyleSet<ModelStyle> modelStyleSet = new StyleSet<ModelStyle>(null);
        modelStyleSet.setZoomStyle(14, modelStyle);

        // create layer and an model
        NMLModelLayer milkTruckLayer = new NMLModelLayer(new EPSG3857());
        try {
            InputStream is = this.getResources().openRawResource(R.raw.milktruck);
            NMLPackage.Model nmlModel = NMLPackage.Model.parseFrom(is);
            // set initial position for the milk truck
            MapPos mapPos = milkTruckLayer.getProjection().fromWgs84(20.466027f, 44.810537f);
            milkTruck = new NMLModel(mapPos, null, modelStyleSet, nmlModel, null);
            // set size, 10 is clear oversize, but this makes it visible
            milkTruck.setScale(new Vector(10, 10, 10));
            milkTruckLayer.add(milkTruck);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        mapView.getLayers().addLayer(milkTruckLayer);

     // Add or modify GPS listener, so milk truck will have new location

        LocationListener locationListener = new LocationListener() 
        {
            public void onLocationChanged(Location location) {
                 if(milkTruck != null){
                     MapPos mapPos = milkTruckLayer.getProjection().fromWgs84(location.getLongitude(), location.getLatitude());
                     milkTruck.setMapPos(mapPos);
                 }
                 
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {}

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };
        
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 100, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
Clone this wiki locally