LoginSignup
3
4

More than 5 years have passed since last update.

Google Maps APIを用いて指定された緯度経度範囲内にランダムなポリラインを描画する

Posted at

個人的なメモ

swift.swift
var polyline: GMSPolyline = GMSPolyline()
func createDummyData() {
    let path: GMSMutablePath = GMSMutablePath()

    // 1000本ランダムで作る
    for _ in 0...1000 {
        let a = getRandomNumber(Min: 34.693329, Max: 34.708026)
        let b = getRandomNumber(Min: 135.496979, Max: 135.507312)
        print("#### \(a), \(b)")         
        let coordinate = CLLocationCoordinate2DMake(CLLocationDegrees(a), CLLocationDegrees(b))
        path.add(coordinate)
    }

    polyline.map = nil
    polyline.strokeColor = UIColor(red: 1.0, green: 0, blue: 0, alpha: 0.5)
    polyline.strokeWidth = 3
    polyline.path = path
    polyline.map = googleMap
    self.googleMap.moveCamera(GMSCameraUpdate.fit(GMSCoordinateBounds(path: path)))
}

func getRandomNumber(Min _Min : Float, Max _Max : Float)->Float {
    return ( Float(arc4random_uniform(UINT32_MAX)) / Float(UINT32_MAX) ) * (_Max - _Min) + _Min
}
java.java
private void createDummyData() {
        ArrayList<LatLng> locationList = new ArrayList<>();
        mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(34.7025505, 135.500997)));
        for (int i = 0; i < 750; i++) {
            locationList.add(new LatLng(getRandomLatLng(34.693329, 34.708026), getRandomLatLng(135.496979, 135.507312)));
        }

        PolylineOptions rectOptions = new PolylineOptions()
                .addAll(locationList)
                .color(Color.parseColor("#59ff0000"));
        Polyline polyline = mMap.addPolyline(rectOptions);
    }

    private double getRandomLatLng(double min, double max) {
        double x = (Math.random()*((max-min)))+min; //    This Will Create A Random Number Inbetween Your Min And Max.
        double xrounded = Math.round(x * 1000000.0) / 1000000.0; // Creates Answer To The Nearest 100th, You Can Modify This To Change How It Rounds.
        System.out.println(xrounded); //    This Will Now Print Out The Rounded, Random Number.
        return xrounded;
    }
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4